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
"
: ([^\s]+)(?:[^\[]+)?\[([^:/]+[:/][^\s]+)(?:\s*l.[\d]+)?\] - ([a-f0-9]{24})?(?: - )?(.+)$
"
gim
:
matches the characters : literally (case insensitive)
1st Capturing Group
([^\s]+)
Match a single character not present in the list below
[^\s]
+ 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  ])
Non-capturing group
(?:[^\[]+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character not 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 9110 (5B16 or 1338) literally (case insensitive)
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case insensitive)
2nd Capturing Group
([^:/]+[:/][^\s]+)
Match a single character not 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 a single character in the list :/ (case insensitive)
Match a single character present in the list below
[:/]
:/
matches a single character in the list :/ (case insensitive)
Match a single character not present in the list below
[^\s]
+ 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  ])
Non-capturing group
(?:\s*l.[\d]+)?
? matches the previous token between zero and one 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 zero and unlimited times, as many times as possible, giving back as needed (greedy)
l matches the character l with index 10810 (6C16 or 1548) literally (case insensitive)
. matches any character (except for line terminators)
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 9310 (5D16 or 1358) literally (case insensitive)
-
matches the characters - literally (case insensitive)
3rd Capturing Group
([a-f0-9]{24})?
? 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
[a-f0-9]
{24} matches the previous token exactly 24 times
a-f matches a single character in the range between a (index 97) and f (index 102) (case insensitive)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case insensitive)
Non-capturing group
(?: - )?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
-
matches the characters - literally (case insensitive)
4th Capturing Group
(.+)
.
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)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
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.

Regular Expression
No Match

r"
"
gim

Test String