(?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: ########## LINE SKIPPER / COUNTER ############
Non-Capturing Group(?: # start non-capture group
# the aim is to skip lines that don't contain blue
# and capture a corresponding digit sequence
(?: # skip one line that doesn't contain blue
^ # beginning of line
(?:(?!blue)[^\r\n])* # zero or more chars
# that do not start blue
(?:\r?\n) # newline chars
)
# With each line skipped, let Group 1 capture
# an ever-increasing portion of the string of numbers
(?= # lookahead
[^~]+ # skip all chars that are not tildes
( # start Group 1
(?(1)\1) # if Group 1 is set, match Group 1
# (?>\1?) # alternate phrasing for the above
~\d+ # match a tilde and digits
) # end Group 1
) # end lookahead
)*+ *+repeats the group contents below any number of times:
Comment: start non-capture group
Comment: the aim is to skip lines that don't contain blue
Comment: and capture a corresponding digit sequence
Non-Capturing Group(?: # skip one line that doesn't contain blue
^ # beginning of line
(?:(?!blue)[^\r\n])* # zero or more chars
# that do not start blue
(?:\r?\n) # newline chars
) Comment: skip one line that doesn't contain blue
^start of line
Comment: beginning 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
Comment: that do not start blue
Non-Capturing Group(?:\r?\n) \r?optionally matches a carriage return (ASCII 13)
\na line-feed (newline) character (ASCII 10)
Comment: newline chars
Comment: With each line skipped, let Group 1 capture
Comment: an ever-increasing portion of the string of numbers
Positive Lookahead(?= # lookahead
[^~]+ # skip all chars that are not tildes
( # start Group 1
(?(1)\1) # if Group 1 is set, match Group 1
# (?>\1?) # alternate phrasing for the above
~\d+ # match a tilde and digits
) # end Group 1
) Comment: lookahead
Negated Character Class[^~]+ +matches at least one character outside the set below:
~the literal ~ (12610 / 1768 / 7E16)
Comment: skip all chars that are not tildes
Group 1( # start Group 1
(?(1)\1) # if Group 1 is set, match Group 1
# (?>\1?) # alternate phrasing for the above
~\d+ # match a tilde and digits
) Comment: start Group 1
Uses the first branch if group 1 matched
If the condition is true, match this branch:\1 \1the text saved by group 1
Comment: if Group 1 is set, match Group 1
Comment: (?>\1?) # alternate phrasing for the above
~the literal text ~
\d+a digit, repeated at least once
Comment: match a tilde and digits
Comment: end Group 1
Comment: end lookahead
Comment: end counter-line-skipper: zero or more times
Comment: the possessive + forbids backtracking
.*?any character, repeated any number of times
Comment: lazily match any chars up to...
blue the literal text blue (case sensitive) Comment: match blue
Negated Character Class[^~]+ +matches at least one character outside the set below:
~the literal ~ (12610 / 1768 / 7E16)
Comment: match any non-tilde chars
Uses the first branch if group 1 matched
If the condition is true, match this branch:\1 \1the text saved by group 1
Comment: if Group 1 has been set, match it
Comment: \1? # alternate phrasing for the above
Comment: match a tilde
\Kresets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
Comment: drop what we matched so far
\d+a digit, repeated at least once
Comment: match digits. This is the match!