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
/
.+HappyMK\s?(?<action>\S+)\s(?<chain>\w+):\sin:(?<int_in>\S+)\sout:(?<int_out>\S+), src-mac\s(?<src_mac>\S+),\sproto\s(?<prot>\w+)(\s(?<flags>\S+),|,)\s(?<src_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<src_port>\d+)->(?<dest_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<dest_port>\d+),(\s(?<NAT>\w+)\s\((?<src_nat_localip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<src_nat_local_port>\d+)->(?<src_nat_public_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<src_nat_public_port>\d+)\)->(?<dest_nat_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<dest_nat_port>\d+),)?(\sprio\s(?<prio>\d+->\d+),)?\slen\s(?<len>\d+)
/
.
matches any character (except for line terminators)
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
HappyMK
matches the characters HappyMK literally (case sensitive)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Named Capture Group action
(?<action>\S+)
\S
matches any non-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)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group chain
(?<chain>\w+)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
in:
matches the characters in: literally (case sensitive)
Named Capture Group int_in
(?<int_in>\S+)
\S
matches any non-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)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
out:
matches the characters out: literally (case sensitive)
Named Capture Group int_out
(?<int_out>\S+)
\S
matches any non-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)
, src-mac
matches the characters , src-mac literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group src_mac
(?<src_mac>\S+)
\S
matches any non-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)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
proto
matches the characters proto literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group prot
(?<prot>\w+)
7th Capturing Group
(\s(?<flags>\S+),|,)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group src_ip
(?<src_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Named Capture Group src_port
(?<src_port>\d+)
->
matches the characters -> literally (case sensitive)
Named Capture Group dest_ip
(?<dest_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Named Capture Group dest_port
(?<dest_port>\d+)
, matches the character , with index 4410 (2C16 or 548) literally (case sensitive)
13th Capturing Group
(\s(?<NAT>\w+)\s\((?<src_nat_localip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<src_nat_local_port>\d+)->(?<src_nat_public_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<src_nat_public_port>\d+)\)->(?<dest_nat_ip>\b(?:\d{1,3}\.){3}\d{1,3}\b):(?<dest_nat_port>\d+),)?
21st Capturing Group
(\sprio\s(?<prio>\d+->\d+),)?
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
len
matches the characters len literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group len
(?<len>\d+)
Your regular expression does not match the subject string.Try launching the debugger to find out why.

Regular Expression
No Match

/
/

Test String