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
/
(?x) ^\s*(\d+)\s+ # coupon number ([OX]) # stopover flag (\w{3})\s+ # depapt (\w{2})\s* # cr (\d{1,4}) # flnr ([A-Z0-9]{2}|\s+)\s+ # operating carrier ([A-Z])\s+ # booking class (\d{2}\w{3}\d{4})\s+ # dep datetime (\w{2})\s+ # status ([\w\d\/]+)\s+ # farebase (\w)\s+ # coupon status (\d{2}\w{3}|\s) # nvb (\d{2}\w{3}|\s)\s+ # nva ([A-Z\d]{2,3}) # baggage allowance
/
mg
(?x)
match the remainder of the pattern with the following effective flags: mgx
x modifier: extended. Spaces and text after a # in the pattern are ignored
^ asserts position at start of a line
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
1st Capturing Group
(\d+)
\d
matches 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)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
# coupon number
coupon number
Comment: coupon number
2nd Capturing Group
([OX])
Match a single character present in the list below
[OX]
OX
matches a single character in the list OX (case sensitive)
# stopover flag
stopover flag
Comment: stopover flag
3rd Capturing Group
(\w{3})
\w
matches any word character (equivalent to [a-zA-Z0-9_])
{3} matches the previous token exactly 3 times
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
# depapt
depapt
Comment: depapt
4th Capturing Group
(\w{2})
\w
matches any word character (equivalent to [a-zA-Z0-9_])
{2} matches the previous token exactly 2 times
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# cr
cr
5th Capturing Group
(\d{1,4})
# flnr
flnr
6th Capturing Group
([A-Z0-9]{2}|\s+)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# operating carrier
operating carrier
7th Capturing Group
([A-Z])
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# booking class
booking class
8th Capturing Group
(\d{2}\w{3}\d{4})
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# dep datetime
dep datetime
9th Capturing Group
(\w{2})
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# status
status
10th Capturing Group
([\w\d\/]+)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# farebase
farebase
11th Capturing Group
(\w)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# coupon status
coupon status
12th Capturing Group
(\d{2}\w{3}|\s)
# nvb
nvb
13th Capturing Group
(\d{2}\w{3}|\s)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
# nva
nva
14th Capturing Group
([A-Z\d]{2,3})
Comment: baggage allowance
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/
mg

Test String