(?xsm)applies the following effective flags to the remainder of the pattern: xsm x modifier: extended. Ignores unescaped whitespace and comments outside character classes
s modifier: single line. Allows a dot to match line terminators
m modifier: multiline. Causes ^ and $ to match the start and end of each line, not only the start and end of the string
Comment: free-spacing mode, DOTALL, multi-line
Positive Lookahead(?=.*?blue) .*?any character, repeated any number of times
bluethe literal text blue (case sensitive) Comment: if blue isn't there, fail without delay
Comment: ##### Recursive Section ######
Comment: This section aims to balance empty lines with digits, i.e.
Comment: emptyLine,emptyLine,emptyLine ... ~1~2~3
Comment: The last digit block is captured to Group 2, e.g. ~3
Positive Lookahead(?= # lookahead
( # Group 1
(?: # skip one line that doesn't contain blue
^ # start of line
(?:(?!blue)[^\r\n])* # zero or more chars not followed by blue
(?:\r?\n) # newline
)
(?:(?1)|[^~]+) # recurse Group 1 OR match all non-tilde chars
(~\d+) # match a sequence of digits
)? # End Group 1
) Comment: lookahead
Group 1( # Group 1
(?: # skip one line that doesn't contain blue
^ # start of line
(?:(?!blue)[^\r\n])* # zero or more chars not followed by blue
(?:\r?\n) # newline
)
(?:(?1)|[^~]+) # recurse Group 1 OR match all non-tilde chars
(~\d+) # match a sequence of digits
)? ?repeats the group contents below at most once:
Comment: Group 1
Non-Capturing Group(?: # skip one line that doesn't contain blue
^ # start of line
(?:(?!blue)[^\r\n])* # zero or more chars not followed by blue
(?:\r?\n) # newline
) Comment: skip one line that doesn't contain blue
^start of line
Comment: start of line
Non-Capturing Group(?:(?!blue)[^\r\n])* *repeats the group contents below any number of times:
Negative Lookahead(?!blue) bluethe literal text blue (case sensitive) Negated Character Class[^\r\n] \ra carriage return (ASCII 13)
\na line-feed (newline) character (ASCII 10)
Comment: zero or more chars not followed by blue
Non-Capturing Group(?:\r?\n) \r?optionally matches a carriage return (ASCII 13)
\na line-feed (newline) character (ASCII 10)
Comment: newline
Non-Capturing Group(?:(?1)|[^~]+) (?1)calls group 1 as a subroutine
Negated Character Class[^~]+ +matches at least one character outside the set below:
~the literal ~ (12610 / 1768 / 7E16)
Comment: recurse Group 1 OR match all non-tilde chars
~the literal ~ (12610 / 1768 / 7E16)
\d+a digit, repeated at least once
Comment: match a sequence of digits
Comment: End Group 1
Comment: End lookahead.
Comment: Group 2, if set, now contains the number of lines skipped
.*?any character, repeated any number of times
Comment: lazily match chars up to...
blue the literal text blue (case sensitive) Comment: match blue
.*?any character, repeated any number of times
Comment: lazily match chars up to...
Uses the first branch if group 2 matched
If the condition is true, match this branch:\2 \2the text saved by group 2
Comment: if Group 2 is set, match Group 2
Comment: Match the next tilde
\Kresets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
Comment: drop what was matched so far
\d+a digit, repeated at least once
Comment: match the next digits: this is the match