Regular Expressions 101

Save & Manage Regex

  • Current Version: 1
  • Save & Share
  • Community Library

Flavor

  • PCRE2 (PHP)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java
  • .NET 7.0 (C#)
  • Rust
  • PCRE (Legacy)
  • Regex Flavor Guide

Function

  • Match
  • Substitution
  • List
  • Unit Tests
Sponsors
An explanation of your regex will be automatically generated as you type.
Detailed match information will be displayed here automatically.
  • All Tokens
  • Common Tokens
  • General Tokens
  • Anchors
  • Meta Sequences
  • Quantifiers
  • Group Constructs
  • Character Classes
  • Flags/Modifiers
  • Substitution
  • A single character of: a, b or c
    [abc]
  • A character except: a, b or c
    [^abc]
  • A character in the range: a-z
    [a-z]
  • A character not in the range: a-z
    [^a-z]
  • A character in the range: a-z or A-Z
    [a-zA-Z]
  • Any single character
    .
  • Alternate - match either a or b
    a|b
  • Any whitespace character
    \s
  • Any non-whitespace character
    \S
  • Any digit
    \d
  • Any non-digit
    \D
  • Any word character
    \w
  • Any non-word character
    \W
  • Non-capturing group
    (?:...)
  • Capturing group
    (...)
  • Zero or one of a
    a?
  • Zero or more of a
    a*
  • One or more of a
    a+
  • Exactly 3 of a
    a{3}
  • 3 or more of a
    a{3,}
  • Between 3 and 6 of a
    a{3,6}
  • Start of string
    ^
  • End of string
    $
  • A word boundary
    \b
  • Non-word boundary
    \B

Regular Expression
Processing...

Test String

Code Generator

Generated Code

import re regex = re.compile(r""" ^ (?:4[0-9]{12}(?:[0-9]{3})? | 5[1-5][0-9]{14} | 6(?:011|5[0-9][0-9])[0-9]{12} | 3[47][0-9]{13} | 3(?:0[0-5]|[68][0-9])[0-9]{11} | (?:2100|2131|1800|35\d{3})\d{11}) $ """, flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE) test_str = ("#American Express\n" "370517943574132\n" "372714451742486\n" "370010255141385\n" "341263547614307\n" "343874494387669\n\n" "#VISA\n" "4024007125780444\n" "4439944519233615\n" "4658355677043536\n" "4532926168018906\n" "4532249806735728\n\n" "#MasterCard\n" "5524097521691644\n" "5367170623993901\n" "5553103980950937\n" "5549194987582424\n" "5141794881796756\n\n" "#JCB 15 digits\n" "180078244412845\n" "210013400722277\n" "210082510016250\n" "180056142071970\n" "210043823226606\n\n" "#JCB\n" "3158822586903214\n" "3088687202983378\n" "3158899851849561\n" "3096803356450490\n" "3337852908456769\n\n" "#Dinners Club\n" "30193567772121\n" "30131361923813\n" "30198560976769\n" "30260244203356\n" "36297440059376\n\n\n") matches = regex.finditer(test_str) for match_num, match in enumerate(matches, start=1): print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}") for group_num, group in enumerate(match.groups(), start=1): print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")

Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html