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
/
^\[([\d]{1,2}-[\w]{3,9}-[\d]{4}\s+[\d]{2}:[\d]{2}:[\d]{2}\s+[A-Z]{3,4})\]\s((?s).+?)(?=(?:^\[.+\])|\Z)
/
gm
^ asserts position at start of a line
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
1st Capturing Group
([\d]{1,2}-[\w]{3,9}-[\d]{4}\s+[\d]{2}:[\d]{2}:[\d]{2}\s+[A-Z]{3,4})
Match a single character present in the list below
[\d]
{1,2} matches the previous token between 1 and 2 times, as many times as possible, giving back as needed (greedy)
\d matches a digit (equivalent to [0-9])
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
Match a single character present in the list below
[\w]
{3,9} matches the previous token between 3 and 9 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)
Match a single character present in the list below
[\d]
{4} matches the previous token exactly 4 times
\d matches a digit (equivalent to [0-9])
\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)
Match a single character present in the list below
[\d]
{2} matches the previous token exactly 2 times
\d matches a digit (equivalent to [0-9])
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below
[\d]
{2} matches the previous token exactly 2 times
\d matches a digit (equivalent to [0-9])
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below
[\d]
{2} matches the previous token exactly 2 times
\d matches a digit (equivalent to [0-9])
\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)
Match a single character present in the list below
[A-Z]
{3,4} matches the previous token between 3 and 4 times, as many times as possible, giving back as needed (greedy)
A-Z matches a single character in the range between A (index 65) and Z (index 90) (case sensitive)
\] matches the character ] with index 9310 (5D16 or 1358) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
2nd Capturing Group
((?s).+?)
(?s)
match the remainder of the pattern with the following effective flags: gms
s modifier: single line. Dot matches newline characters
.
matches any character
+? matches the previous token between one and unlimited times, as few times as possible, expanding as needed (lazy)
Positive Lookahead
(?=(?:^\[.+\])|\Z)
Assert that the Regex below matches
1st Alternative
(?:^\[.+\])
Non-capturing group
(?:^\[.+\])
^ asserts position at start of a line
\[ matches the character [ with index 9110 (5B16 or 1338) literally (case sensitive)
.
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)
2nd Alternative
\Z
\Z asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
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