Regular Expressions 101

Save & Share

  • Save Regex
    ctrl+s
  • Update Regex
    ctrl+⇧+s

Flavor

  • PCRE2 (PHP >=7.3)
  • PCRE (PHP <7.3)
  • ECMAScript (JavaScript)
  • Python
  • Golang
  • Java 8

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?(DEFINE) (?<sep> [ -]?) ) (?<!\d)(?: \d{4} (?&sep) \d{4} (?&sep) \d{4} (?&sep) \d{4} # 16 digits | \d{3} (?&sep) \d{3} (?&sep) \d{3} (?&sep) \d (?&sep) \d{3} # 13 digits | \d{4} (?&sep) \d{6} (?&sep) \d{4} # 14 digits | \d{4} (?&sep) \d{6} (?&sep) \d{5} # 15 digit card )(?!\d)
/
gx
Subpattern definition construct
(?(DEFINE) (?<sep> [ -]?) )
May only be used to define functions. No matching is done in this group.
Named Capture Group sep
(?<sep> [ -]?)
Match a single character present in the list below
[ -]
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
-
matches a single character in the list - (case sensitive)
Negative Lookbehind
(?<!\d)
Assert that the Regex below does not match
\d matches a digit (equivalent to [0-9])
Non-capturing group
(?: \d{4} (?&sep) \d{4} (?&sep) \d{4} (?&sep) \d{4} # 16 digits | \d{3} (?&sep) \d{3} (?&sep) \d{3} (?&sep) \d (?&sep) \d{3} # 13 digits | \d{4} (?&sep) \d{6} (?&sep) \d{4} # 14 digits | \d{4} (?&sep) \d{6} (?&sep) \d{5} # 15 digit card )
1st Alternative
\d{4} (?&sep) \d{4} (?&sep) \d{4} (?&sep) \d{4} # 16 digits
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
# 16 digits
16 digits
Comment: 16 digits
2nd Alternative
\d{3} (?&sep) \d{3} (?&sep) \d{3} (?&sep) \d (?&sep) \d{3} # 13 digits
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
(?&sep) recurses the subpattern named sep
\d matches a digit (equivalent to [0-9])
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
# 13 digits
13 digits
Comment: 13 digits
3rd Alternative
\d{4} (?&sep) \d{6} (?&sep) \d{4} # 14 digits
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{6} matches the previous token exactly 6 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
# 14 digits
14 digits
Comment: 14 digits
4th Alternative
\d{4} (?&sep) \d{6} (?&sep) \d{5} # 15 digit card
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{6} matches the previous token exactly 6 times
(?&sep) recurses the subpattern named sep
\d
matches a digit (equivalent to [0-9])
{5} matches the previous token exactly 5 times
# 15 digit card
15 digit card
Comment: 15 digit card
Negative Lookahead
(?!\d)
Assert that the Regex below does not match
\d matches a digit (equivalent to [0-9])
Global pattern flags
g modifier: global. All matches (don't return after first match)
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
gx

Test String