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
"
\s(?=\w*[0-9])(?=\w*[A-Z])([A-Z\d]{11}|[A-Z\d]{7})\s
"
g
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Positive Lookahead
(?=\w*[0-9])
Assert that the Regex below matches
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
Positive Lookahead
(?=\w*[A-Z])
Assert that the Regex below matches
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below
[A-Z]
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
1st Capturing Group
([A-Z\d]{11}|[A-Z\d]{7})
1st Alternative
[A-Z\d]{11}
Match a single character present in the list below
[A-Z\d]
{11} matches the previous token exactly 11 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\d matches a digit (equivalent to [0-9])
2nd Alternative
[A-Z\d]{7}
Match a single character present in the list below
[A-Z\d]
{7} matches the previous token exactly 7 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\d matches a digit (equivalent to [0-9])
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Global pattern flags
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.

Regular Expression
No Match

"
"
g

Test String