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
/
^ (?<FirstLine> # First line capturing group (?<Passport>P) # Passport character capturing group (P char, length 1) (?<PassportType>.) # Passport Type (any char, generally <, length 1) (?<IssuingCountry>[AFG]{3}) # To be completed with the ISO 3166-1 alpha-3 country codes (length 3) # Or in alternative it can be checked for char only as # (?<IssuingCountry>[A-Z<]{3}) if check state not necessary (?<PassportOwner> # Passport owner capturing group !Does not check for length 39! (?<Surname>[A-Z]+) # Surname, it has to be followed by << << (?<GivenName> # Given Name (?:[A-Z]+<?)+ ) [<]+ ){1} ) \n (?<SecondLine> # Second Line capturing group (?<PassportNumber>[A-Z0-9<]{9}) # Passport number, length 9, padded with < (?<CheckDigit19>[0-9]{1}) # Check digit for position 1 to 9 (?<Nationality>\g{IssuingCountry}) # Nationality, follows the same rule as match group 4 (?<DoB> # Date Of Birth (?<DoBYear>[0-9]{2}) (?<DoBMonth>(?:0[1-9]|1[0-2])) (?<DoBDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) ) (?<CheckDigit1419>[0-9]) # Check digit for position 14 to 19 (?<Sex>[MF<]) # Sex (Male, Female or not defined) (?<Expiral> # Expiral date (?<ExpiralYear>[0-9]{2}) # W (?<ExpiralMonth>(?:0[1-9]|1[0-2])) (?<ExpiralDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) ) (?<CheckDigit2227>[0-9]) # Check digit for position 22 to 27 (?<PersonalNumber>[A-Z0-9<]{14}) # Personal number padded with < (?<CheckDigit2942>[0-9<]) # Check digit for position 29 to 42 (can be < empty) (?<CheckDigitF>[0-9]) # Check digit ) $
/
xm
^ asserts position at start of a line
Named Capture Group FirstLine
(?<FirstLine> # First line capturing group (?<Passport>P) # Passport character capturing group (P char, length 1) (?<PassportType>.) # Passport Type (any char, generally <, length 1) (?<IssuingCountry>[AFG]{3}) # To be completed with the ISO 3166-1 alpha-3 country codes (length 3) # Or in alternative it can be checked for char only as # (?<IssuingCountry>[A-Z<]{3}) if check state not necessary (?<PassportOwner> # Passport owner capturing group !Does not check for length 39! (?<Surname>[A-Z]+) # Surname, it has to be followed by << << (?<GivenName> # Given Name (?:[A-Z]+<?)+ ) [<]+ ){1} )
# First line capturing group
First line capturing group
Comment: First line capturing group
Named Capture Group Passport
(?<Passport>P)
P matches the character P with index 8010 (5016 or 1208) literally (case sensitive)
# Passport character capturing group (P char, length 1)
Passport character capturing group (P char, length 1)
Comment: Passport character capturing group (P char, length 1)
Named Capture Group PassportType
(?<PassportType>.)
. matches any character (except for line terminators)
# Passport Type (any char, generally <, length 1)
Passport Type (any char, generally <, length 1)
Comment: Passport Type (any char, generally <, length 1)
Named Capture Group IssuingCountry
(?<IssuingCountry>[AFG]{3})
Match a single character present in the list below
[AFG]
{3} matches the previous token exactly 3 times
AFG
matches a single character in the list AFG (case sensitive)
# To be completed with the ISO 3166-1 alpha-3 country codes (length 3)
To be completed with the ISO 3166-1 alpha-3 country codes (length 3)
Comment: To be completed with the ISO 3166-1 alpha-3 country codes (length 3)
# Or in alternative it can be checked for char only as
Or in alternative it can be checked for char only as
Comment: Or in alternative it can be checked for char only as
# (?<IssuingCountry>[A-Z<]{3}) if check state not necessary
(?<IssuingCountry>[A-Z<]{3}) if check state not necessary
Comment: (?<IssuingCountry>[A-Z<]{3}) if check state not necessary
Named Capture Group PassportOwner
(?<PassportOwner> # Passport owner capturing group !Does not check for length 39! (?<Surname>[A-Z]+) # Surname, it has to be followed by << << (?<GivenName> # Given Name (?:[A-Z]+<?)+ ) [<]+ ){1}
{1} matches the previous token exactly one time (meaningless quantifier)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
# Passport owner capturing group !Does not check for length 39!
Passport owner capturing group !Does not check for length 39!
Comment: Passport owner capturing group !Does not check for length 39!
Named Capture Group Surname
(?<Surname>[A-Z]+)
Match a single character present in the list below
[A-Z]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
# Surname, it has to be followed by << <<
Surname, it has to be followed by <<
Named Capture Group GivenName
(?<GivenName> # Given Name (?:[A-Z]+<?)+ )
Match a single character present in the list below
[<]
\n matches a line-feed (newline) character (ASCII 10)
Named Capture Group SecondLine
(?<SecondLine> # Second Line capturing group (?<PassportNumber>[A-Z0-9<]{9}) # Passport number, length 9, padded with < (?<CheckDigit19>[0-9]{1}) # Check digit for position 1 to 9 (?<Nationality>\g{IssuingCountry}) # Nationality, follows the same rule as match group 4 (?<DoB> # Date Of Birth (?<DoBYear>[0-9]{2}) (?<DoBMonth>(?:0[1-9]|1[0-2])) (?<DoBDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) ) (?<CheckDigit1419>[0-9]) # Check digit for position 14 to 19 (?<Sex>[MF<]) # Sex (Male, Female or not defined) (?<Expiral> # Expiral date (?<ExpiralYear>[0-9]{2}) # W (?<ExpiralMonth>(?:0[1-9]|1[0-2])) (?<ExpiralDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) ) (?<CheckDigit2227>[0-9]) # Check digit for position 22 to 27 (?<PersonalNumber>[A-Z0-9<]{14}) # Personal number padded with < (?<CheckDigit2942>[0-9<]) # Check digit for position 29 to 42 (can be < empty) (?<CheckDigitF>[0-9]) # Check digit )
# Second Line capturing group
Second Line capturing group
Comment: Second Line capturing group
Named Capture Group PassportNumber
(?<PassportNumber>[A-Z0-9<]{9})
Match a single character present in the list below
[A-Z0-9<]
{9} matches the previous token exactly 9 times
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
< matches the character < with index 6010 (3C16 or 748) literally (case sensitive)
# Passport number, length 9, padded with <
Passport number, length 9, padded with <
Comment: Passport number, length 9, padded with <
Named Capture Group CheckDigit19
(?<CheckDigit19>[0-9]{1})
Match a single character present in the list below
[0-9]
{1} matches the previous token exactly one time (meaningless quantifier)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
# Check digit for position 1 to 9
Check digit for position 1 to 9
Comment: Check digit for position 1 to 9
Named Capture Group Nationality
(?<Nationality>\g{IssuingCountry})
\g{IssuingCountry} matches the same text as most recently matched by the capturing group named IssuingCountry
# Nationality, follows the same rule as match group 4
Nationality, follows the same rule as match group 4
Comment: Nationality, follows the same rule as match group 4
Named Capture Group DoB
(?<DoB> # Date Of Birth (?<DoBYear>[0-9]{2}) (?<DoBMonth>(?:0[1-9]|1[0-2])) (?<DoBDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) )
# Date Of Birth
Date Of Birth
Comment: Date Of Birth
Named Capture Group DoBYear
(?<DoBYear>[0-9]{2})
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)
Named Capture Group DoBMonth
(?<DoBMonth>(?:0[1-9]|1[0-2]))
Non-capturing group
(?:0[1-9]|1[0-2])
1st Alternative
0[1-9]
0 matches the character 0 with index 4810 (3016 or 608) 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
1[0-2]
Named Capture Group DoBDay
(?<DoBDay>(?:0[1-9]|(?:1|2)[0-9]|3[01]))
Named Capture Group CheckDigit1419
(?<CheckDigit1419>[0-9])
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)
# Check digit for position 14 to 19
Check digit for position 14 to 19
Comment: Check digit for position 14 to 19
Named Capture Group Sex
(?<Sex>[MF<])
# Sex (Male, Female or not defined)
Sex (Male, Female or not defined)
Named Capture Group Expiral
(?<Expiral> # Expiral date (?<ExpiralYear>[0-9]{2}) # W (?<ExpiralMonth>(?:0[1-9]|1[0-2])) (?<ExpiralDay>(?:0[1-9]|(?:1|2)[0-9]|3[01])) )
Named Capture Group CheckDigit2227
(?<CheckDigit2227>[0-9])
# Check digit for position 22 to 27
Check digit for position 22 to 27
Named Capture Group PersonalNumber
(?<PersonalNumber>[A-Z0-9<]{14})
# Personal number padded with <
Personal number padded with <
Named Capture Group CheckDigit2942
(?<CheckDigit2942>[0-9<])
# Check digit for position 29 to 42 (can be < empty)
Check digit for position 29 to 42 (can be < empty)
Named Capture Group CheckDigitF
(?<CheckDigitF>[0-9])
# Check digit
Check digit
$ asserts position at the end of a line
Global pattern flags
x modifier: extended. Spaces and text after a # in the pattern are ignored
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
xm

Test String

Substitution

Processing...