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<host>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})(\s-\s)(?P<user_name>[\w-]*)(\s\[)(?P<time>\w*\/\w*\/\d{1,4}:\d{1,2}:\d{1,2}:\d{1,2}\s-\d{1,4})(\]\s\")(?P<request>.*)(")
/
gm
Named Capture Group host
(?P<host>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
. matches any character (except for line terminators)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
. matches any character (except for line terminators)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
. matches any character (except for line terminators)
\d
matches a digit (equivalent to [0-9])
{1,3} matches the previous token between 1 and 3 times, as many times as possible, giving back as needed (greedy)
2nd Capturing Group
(\s-\s)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
Named Capture Group user_name
(?P<user_name>[\w-]*)
Match a single character present in the list below
[\w-]
* matches the previous token between zero 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 4510 (2D16 or 558) literally (case sensitive)
4th Capturing Group
(\s\[)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
Named Capture Group time
(?P<time>\w*\/\w*\/\d{1,4}:\d{1,2}:\d{1,2}:\d{1,2}\s-\d{1,4})
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,2} matches the previous token between 1 and 2 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 character - with index 4510 (2D16 or 558) literally (case sensitive)
\d
matches a digit (equivalent to [0-9])
{1,4} matches the previous token between 1 and 4 times, as many times as possible, giving back as needed (greedy)
6th Capturing Group
(\]\s\")
\] 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 character " with index 3410 (2216 or 428) literally (case sensitive)
Named Capture Group request
(?P<request>.*)
.
matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
8th Capturing Group
(")
" matches the character " with index 3410 (2216 or 428) literally (case sensitive)
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