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
/
(?P<request_date>^[\w\/\:]+)\s+(?P<request_offset>[\d\+]+)\s+\[(?P<request_id>.+)\]\s+(?P<request_inout>[\-\>\<]+)\s+((?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?\s+(?P<request_path>[^;][^;][a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]+)\s+(?P<request_protocol>.+)\s+|(?P<request_code>\d+)\s+(?P<request_filetype>.+)\s+(?P<request_duration>[\d\w]+)\s+)
/
gm
Named Capture Group request_date
(?P<request_date>^[\w\/\:]+)
^ asserts position at start of a line
Match a single character present in the list below
[\w\/\:]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\w matches any word character (equivalent to [a-zA-Z0-9_])
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\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)
Named Capture Group request_offset
(?P<request_offset>[\d\+]+)
Match a single character present in the list below
[\d\+]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
\+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
\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)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
Named Capture Group request_id
(?P<request_id>.+)
.
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)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\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)
Named Capture Group request_inout
(?P<request_inout>[\-\>\<]+)
Match a single character present in the list below
[\-\>\<]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\> matches the character > with index 6210 (3E16 or 768) literally (case sensitive)
\< matches the character < with index 6010 (3C16 or 748) literally (case sensitive)
\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)
5th Capturing Group
((?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?\s+(?P<request_path>[^;][^;][a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]+)\s+(?P<request_protocol>.+)\s+|(?P<request_code>\d+)\s+(?P<request_filetype>.+)\s+(?P<request_duration>[\d\w]+)\s+)
1st Alternative
(?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?\s+(?P<request_path>[^;][^;][a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]+)\s+(?P<request_protocol>.+)\s+
Named Capture Group request_method
(?P<request_method>[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]+)?
? 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
[GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
GET|POST|HEAD|PUT|DELETE|CONNECT|OPTIONS|TRACE|PATCH
matches a single character in the list GET|POSHADULCNIR (case sensitive)
\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)
Named Capture Group request_path
(?P<request_path>[^;][^;][a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]+)
Match a single character not present in the list below
[^;]
; matches the character ; with index 5910 (3B16 or 738) literally (case sensitive)
Match a single character not present in the list below
[^;]
Match a single character present in the list below
[a-zA-Z0-9\/\_\-\.\=\@\:\%\+\~\#\?\&\{\}\[\]\|]
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group request_protocol
(?P<request_protocol>.+)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
2nd Alternative
(?P<request_code>\d+)\s+(?P<request_filetype>.+)\s+(?P<request_duration>[\d\w]+)\s+
Named Capture Group request_code
(?P<request_code>\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)
Named Capture Group request_filetype
(?P<request_filetype>.+)
\s
matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group request_duration
(?P<request_duration>[\d\w]+)
\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)
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

/
/
gm

Test String