Regular Expressions 101

Save & Share

Flavor

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

Function

  • Match
  • Substitution
  • List
  • Unit Tests
/
(?# This regex was created for the purposes of detecting potentially compromised credentials for the purposes of information security and not to be used for malicious purposes.) (?<!\d) (?# s - Short [less than 16 digits], m - Medium [16 digits], l - Long [more than 16 digits; not yet implemented]) (?# First four digits) (?: (?# Standard 16 digit numbers) (?<m> (?#MasterCard between 222100-272099) 222[1-9]|223\d|2[3-6]\d{2}|27[01]\d|2720| (?#JCB is between 3528-3589) 352[89]|35[3-8]\d| (?#Visa, MasterCard, Discover) [4-6]\d{3} )| (?#American Express 15 digits | Diners Club 14 digits) (?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2})) ) (?# Rest of the card number) (?<del>\D+)?(?(<m>)\d{4}(?(<del>)\k{del})\d{4}(?(<del>)\k{del})\d{4}|(?:\d{6})(?(<del>)\k{del})(?(<ax>)\d{5}|\d{4})) (?#END) (?!\d)
/
gmx
Comment: This regex was created for the purposes of detecting potentially compromised credentials for the purposes of information security and not to be used for malicious purposes.
Negative Lookbehind
(?<!\d)
Assert that the Regex below does not match
\d matches a digit (equivalent to [0-9])
Comment: s - Short [less than 16 digits], m - Medium [16 digits], l - Long [more than 16 digits; not yet implemented]
Comment: First four digits
Non-capturing group
(?: (?# Standard 16 digit numbers) (?<m> (?#MasterCard between 222100-272099) 222[1-9]|223\d|2[3-6]\d{2}|27[01]\d|2720| (?#JCB is between 3528-3589) 352[89]|35[3-8]\d| (?#Visa, MasterCard, Discover) [4-6]\d{3} )| (?#American Express 15 digits | Diners Club 14 digits) (?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2})) )
1st Alternative
(?# Standard 16 digit numbers) (?<m> (?#MasterCard between 222100-272099) 222[1-9]|223\d|2[3-6]\d{2}|27[01]\d|2720| (?#JCB is between 3528-3589) 352[89]|35[3-8]\d| (?#Visa, MasterCard, Discover) [4-6]\d{3} )
Comment: Standard 16 digit numbers
Named Capture Group m
(?<m> (?#MasterCard between 222100-272099) 222[1-9]|223\d|2[3-6]\d{2}|27[01]\d|2720| (?#JCB is between 3528-3589) 352[89]|35[3-8]\d| (?#Visa, MasterCard, Discover) [4-6]\d{3} )
1st Alternative
(?#MasterCard between 222100-272099) 222[1-9]
Comment: MasterCard between 222100-272099
222
matches the characters 222 literally (case sensitive)
Match a single character present in the list below
[1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
2nd Alternative
223\d
223
matches the characters 223 literally (case sensitive)
\d matches a digit (equivalent to [0-9])
3rd Alternative
2[3-6]\d{2}
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below
[3-6]
3-6 matches a single character in the range between 3 (index 51) and 6 (index 54) (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
4th Alternative
27[01]\d
27
matches the characters 27 literally (case sensitive)
Match a single character present in the list below
[01]
01
matches a single character in the list 01 (case sensitive)
\d matches a digit (equivalent to [0-9])
5th Alternative
2720
2720
matches the characters 2720 literally (case sensitive)
6th Alternative
(?#JCB is between 3528-3589) 352[89]
Comment: JCB is between 3528-3589
352
matches the characters 352 literally (case sensitive)
Match a single character present in the list below
[89]
89
matches a single character in the list 89 (case sensitive)
7th Alternative
35[3-8]\d
35
matches the characters 35 literally (case sensitive)
Match a single character present in the list below
[3-8]
3-8 matches a single character in the range between 3 (index 51) and 8 (index 56) (case sensitive)
\d matches a digit (equivalent to [0-9])
8th Alternative
(?#Visa, MasterCard, Discover) [4-6]\d{3}
Comment: Visa, MasterCard, Discover
Match a single character present in the list below
[4-6]
4-6 matches a single character in the range between 4 (index 52) and 6 (index 54) (case sensitive)
\d
matches a digit (equivalent to [0-9])
{3} matches the previous token exactly 3 times
2nd Alternative
(?#American Express 15 digits | Diners Club 14 digits) (?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2}))
Comment: American Express 15 digits | Diners Club 14 digits
Named Capture Group s
(?<s>(?<ax>3[47]\d{2})|(?<dc>30[0-5]\d|36\d{2}))
1st Alternative
(?<ax>3[47]\d{2})
Named Capture Group ax
(?<ax>3[47]\d{2})
3 matches the character 3 with index 5110 (3316 or 638) literally (case sensitive)
Match a single character present in the list below
[47]
47
matches a single character in the list 47 (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
2nd Alternative
(?<dc>30[0-5]\d|36\d{2})
Named Capture Group dc
(?<dc>30[0-5]\d|36\d{2})
1st Alternative
30[0-5]\d
30
matches the characters 30 literally (case sensitive)
Match a single character present in the list below
[0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (case sensitive)
\d matches a digit (equivalent to [0-9])
2nd Alternative
36\d{2}
36
matches the characters 36 literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{2} matches the previous token exactly 2 times
Comment: Rest of the card number
Named Capture Group del
(?<del>\D+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
\D
matches any character that's not a digit (equivalent to [^0-9])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Conditional
(?(<m>)\d{4}(?(<del>)\k{del})\d{4}(?(<del>)\k{del})\d{4}|(?:\d{6})(?(<del>)\k{del})(?(<ax>)\d{5}|\d{4}))
Conditionally matches one of two options depending on whether the m capturing group matched
If condition is met, match the following regex
\d{4}(?(<del>)\k{del})\d{4}(?(<del>)\k{del})\d{4}
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Conditional
(?(<del>)\k{del})
Conditionally matches one of two options depending on whether the del capturing group matched
If condition is met, match the following regex
\k{del}
\k{del} matches the same text as most recently matched by the capturing group named del
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Conditional
(?(<del>)\k{del})
Conditionally matches one of two options depending on whether the del capturing group matched
If condition is met, match the following regex
\k{del}
\k{del} matches the same text as most recently matched by the capturing group named del
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Else match the following regex
(?:\d{6})(?(<del>)\k{del})(?(<ax>)\d{5}|\d{4})
Non-capturing group
(?:\d{6})
\d
matches a digit (equivalent to [0-9])
{6} matches the previous token exactly 6 times
Conditional
(?(<del>)\k{del})
Conditionally matches one of two options depending on whether the del capturing group matched
If condition is met, match the following regex
\k{del}
\k{del} matches the same text as most recently matched by the capturing group named del
Conditional
(?(<ax>)\d{5}|\d{4})
Conditionally matches one of two options depending on whether the ax capturing group matched
If condition is met, match the following regex
\d{5}
\d
matches a digit (equivalent to [0-9])
{5} matches the previous token exactly 5 times
Else match the following regex
\d{4}
\d
matches a digit (equivalent to [0-9])
{4} matches the previous token exactly 4 times
Comment: END
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)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
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

/
/
gmx

Test String