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
/
CA_[A-Z][0-9][A-Z]\h?[0-9][A-Z][0-9]|US_[0-9]{5}|GB_(?:(?:[A-Z][0-9]|[A-Z][0-9]{2}|[A-Z]{2}[0-9]|[A-Z]{2}[0-9]{2}|[A-Z][0-9][A-Z]|[A-Z]{2}[0-9][A-Z])\h?[0-9][A-Z]{2}|GIR\h?0AA)
/
1st Alternative
CA_[A-Z][0-9][A-Z]\h?[0-9][A-Z][0-9]
CA_
matches the characters CA_ literally (case sensitive)
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)
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)
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)
\h
matches any horizontal whitespace character (equivalent to [[:blank:]])
? matches the previous token between zero and one 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)
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)
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)
2nd Alternative
US_[0-9]{5}
US_
matches the characters US_ literally (case sensitive)
Match a single character present in the list below
[0-9]
{5} matches the previous token exactly 5 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative
GB_(?:(?:[A-Z][0-9]|[A-Z][0-9]{2}|[A-Z]{2}[0-9]|[A-Z]{2}[0-9]{2}|[A-Z][0-9][A-Z]|[A-Z]{2}[0-9][A-Z])\h?[0-9][A-Z]{2}|GIR\h?0AA)
GB_
matches the characters GB_ literally (case sensitive)
Non-capturing group
(?:(?:[A-Z][0-9]|[A-Z][0-9]{2}|[A-Z]{2}[0-9]|[A-Z]{2}[0-9]{2}|[A-Z][0-9][A-Z]|[A-Z]{2}[0-9][A-Z])\h?[0-9][A-Z]{2}|GIR\h?0AA)
1st Alternative
(?:[A-Z][0-9]|[A-Z][0-9]{2}|[A-Z]{2}[0-9]|[A-Z]{2}[0-9]{2}|[A-Z][0-9][A-Z]|[A-Z]{2}[0-9][A-Z])\h?[0-9][A-Z]{2}
Non-capturing group
(?:[A-Z][0-9]|[A-Z][0-9]{2}|[A-Z]{2}[0-9]|[A-Z]{2}[0-9]{2}|[A-Z][0-9][A-Z]|[A-Z]{2}[0-9][A-Z])
1st Alternative
[A-Z][0-9]
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)
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)
2nd Alternative
[A-Z][0-9]{2}
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)
Match a single character present in the list below
[0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative
[A-Z]{2}[0-9]
Match a single character present in the list below
[A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
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)
4th Alternative
[A-Z]{2}[0-9]{2}
Match a single character present in the list below
[A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
Match a single character present in the list below
[0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
5th Alternative
[A-Z][0-9][A-Z]
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)
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)
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)
6th Alternative
[A-Z]{2}[0-9][A-Z]
Match a single character present in the list below
[A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
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)
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)
\h
matches any horizontal whitespace character (equivalent to [[:blank:]])
? matches the previous token between zero and one 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)
Match a single character present in the list below
[A-Z]
{2} matches the previous token exactly 2 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
2nd Alternative
GIR\h?0AA
GIR
matches the characters GIR literally (case sensitive)
\h
matches any horizontal whitespace character (equivalent to [[:blank:]])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
0AA
matches the characters 0AA literally (case sensitive)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/

Test String