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
"
^(?:(\\section\{[^\v}]+})\v+)? ^((?:[^\\\v][^\v]+\v*)+)(?<!\v)$
"
gmx
^ asserts position at start of a line
Non-capturing group
(?:(\\section\{[^\v}]+})\v+)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Capturing Group
(\\section\{[^\v}]+})
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
section
matches the characters section literally (case sensitive)
\{ matches the character { with index 12310 (7B16 or 1738) literally (case sensitive)
Match a single character not present in the list below
[^\v}]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\v matches a vertical tab character
} matches the character } with index 12510 (7D16 or 1758) literally (case sensitive)
} matches the character } with index 12510 (7D16 or 1758) literally (case sensitive)
\v
matches a vertical tab character
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
^ asserts position at start of a line
2nd Capturing Group
((?:[^\\\v][^\v]+\v*)+)
Non-capturing group
(?:[^\\\v][^\v]+\v*)+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character not present in the list below
[^\\\v]
\\ matches the character \ with index 9210 (5C16 or 1348) literally (case sensitive)
\v matches a vertical tab character
Match a single character not present in the list below
[^\v]
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\v matches a vertical tab character
\v
matches a vertical tab character
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Negative Lookbehind
(?<!\v)
Assert that the Regex below does not match
\v matches a vertical tab character
$ asserts position at the end of a line
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)
x modifier: extended. Spaces and text after a # in the pattern are ignored
Your regular expression does not match the subject string.

Regular Expression
No Match

r"
"
gmx

Test String