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
/
^([ ]{6})(- )((?!name)\w+[:](?:[ ]*(?=\n\1[ ]{4}\S)|[ ]*\S.+))((?:\n\1[ ]{2}.+)*?)(?:\n(\1[ ]{2})(name[ ]*[:])((?:[ ]*(?![>|])\S.+\b)|(?:(?:[ ]*[>|])?\n\5[ ]+.+)*))[ ]*$
/
gm
^ asserts position at start of a line
1st Capturing Group
([ ]{6})
Match a single character present in the list below
[ ]
{6} matches the previous token exactly 6 times
matches the character with index 3210 (2016 or 408) literally (case sensitive)
2nd Capturing Group
(- )
-
matches the characters - literally (case sensitive)
3rd Capturing Group
((?!name)\w+[:](?:[ ]*(?=\n\1[ ]{4}\S)|[ ]*\S.+))
Negative Lookahead
(?!name)
Assert that the Regex below does not match
name
matches the characters name literally (case sensitive)
\w
matches any word character (equivalent to [a-zA-Z0-9_])
+ 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
[:]
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Non-capturing group
(?:[ ]*(?=\n\1[ ]{4}\S)|[ ]*\S.+)
1st Alternative
[ ]*(?=\n\1[ ]{4}\S)
Match a single character present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Positive Lookahead
(?=\n\1[ ]{4}\S)
Assert that the Regex below matches
\n matches a line-feed (newline) character (ASCII 10)
\1 matches the same text as most recently matched by the 1st capturing group
Match a single character present in the list below
[ ]
{4} matches the previous token exactly 4 times
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
2nd Alternative
[ ]*\S.+
Match a single character present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
.
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)
4th Capturing Group
((?:\n\1[ ]{2}.+)*?)
Non-capturing group
(?:\n\1[ ]{2}.+)*?
*? matches the previous token between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\n matches a line-feed (newline) character (ASCII 10)
\1 matches the same text as most recently matched by the 1st capturing group
Match a single character present in the list below
[ ]
{2} matches the previous token exactly 2 times
matches the character with index 3210 (2016 or 408) 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)
Non-capturing group
(?:\n(\1[ ]{2})(name[ ]*[:])((?:[ ]*(?![>|])\S.+\b)|(?:(?:[ ]*[>|])?\n\5[ ]+.+)*))
\n matches a line-feed (newline) character (ASCII 10)
5th Capturing Group
(\1[ ]{2})
\1 matches the same text as most recently matched by the 1st capturing group
Match a single character present in the list below
[ ]
{2} matches the previous token exactly 2 times
matches the character with index 3210 (2016 or 408) literally (case sensitive)
6th Capturing Group
(name[ ]*[:])
name
matches the characters name literally (case sensitive)
Match a single character present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Match a single character present in the list below
[:]
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
7th Capturing Group
((?:[ ]*(?![>|])\S.+\b)|(?:(?:[ ]*[>|])?\n\5[ ]+.+)*)
1st Alternative
(?:[ ]*(?![>|])\S.+\b)
Non-capturing group
(?:[ ]*(?![>|])\S.+\b)
Match a single character present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Negative Lookahead
(?![>|])
Assert that the Regex below does not match
Match a single character present in the list below
[>|]
>|
matches a single character in the list >| (case sensitive)
\S matches any non-whitespace character (equivalent to [^\r\n\t\f\v \u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff])
.
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)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
2nd Alternative
(?:(?:[ ]*[>|])?\n\5[ ]+.+)*
Non-capturing group
(?:(?:[ ]*[>|])?\n\5[ ]+.+)*
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
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 present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
Match a single character present in the list below
[>|]
>|
matches a single character in the list >| (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
\5 matches the same text as most recently matched by the 5th capturing group
Match a single character 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 3210 (2016 or 408) 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)
Match a single character present in the list below
[ ]
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
matches the character with index 3210 (2016 or 408) literally (case sensitive)
$ 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)
Your regular expression does not match the subject string.

Regular Expression
No Match

/
/
gm

Test String

Substitution

Processing...