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*)\/{4,}\n)+(^(\s*)\/{2,3}(?!\/)[\s\S]*\n)*\1)
/
gm
Non-capturing group
(?:(^(?:\s*)\/{4,}\n)+(^(\s*)\/{2,3}(?!\/)[\s\S]*\n)*\1)
1st Capturing Group
(^(?:\s*)\/{4,}\n)+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
^ asserts position at start of a line
Non-capturing group
(?:\s*)
\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)
\/
matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
{4,} matches the previous token between 4 and unlimited times, as many times as possible, giving back as needed (greedy)
\n matches a line-feed (newline) character (ASCII 10)
2nd Capturing Group
(^(\s*)\/{2,3}(?!\/)[\s\S]*\n)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
^ asserts position at start of a line
3rd Capturing Group
(\s*)
\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)
\/
matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
{2,3} matches the previous token between 2 and 3 times, as many times as possible, giving back as needed (greedy)
Negative Lookahead
(?!\/)
Assert that the Regex below does not match
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
Match a single character present in the list below
[\s\S]
* matches the previous token between zero 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 ])
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v ])
\n matches a line-feed (newline) character (ASCII 10)
\1 matches the same text as most recently matched by the 1st capturing group
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

Substitution

Processing...