(?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, multi-line
Positive Lookahead(?=.*?pig) .*?any character, repeated any number of times
pigthe literal text pig (case sensitive) Comment: lookahead: if pig is not there, fail right away to save the effort
Non-Capturing Group(?: # start counter-line-skipper (lines that don't include pig)
(?: # skip one line that doesn't have pig
^ # beginning of line
(?:(?!pig)[^\r\n])* # zero or more chars not followed by pig
(?:\r?\n) # newline chars
)
# for each line skipped, let Group 1 match an ever increasing portion of the numbers string at the bottom
(?= # lookahead
.* # get to the end of the input
( # start Group 1
:\d+ # match a colon and some digits
(?(1)\1) # if Group 1 is set, match Group 1
) # end Group 1
) # end lookahead
)*+ *+repeats the group contents below any number of times:
Comment: start counter-line-skipper (lines that don't include pig)
Non-Capturing Group(?: # skip one line that doesn't have pig
^ # beginning of line
(?:(?!pig)[^\r\n])* # zero or more chars not followed by pig
(?:\r?\n) # newline chars
) Comment: skip one line that doesn't have pig
^start of line
Comment: beginning of line
Non-Capturing Group(?:(?!pig)[^\r\n])* *repeats the group contents below any number of times:
Negative Lookahead(?!pig) pigthe literal text pig (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 pig
Non-Capturing Group(?:\r?\n) \r?optionally matches a carriage return (ASCII 13)
\na line-feed (newline) character (ASCII 10)
Comment: newline chars
Comment: for each line skipped, let Group 1 match an ever increasing portion of the numbers string at the bottom
Positive Lookahead(?= # lookahead
.* # get to the end of the input
( # start Group 1
:\d+ # match a colon and some digits
(?(1)\1) # if Group 1 is set, match Group 1
) # end Group 1
) Comment: lookahead
.*any character, repeated any number of times
Comment: get to the end of the input
Group 1( # start Group 1
:\d+ # match a colon and some digits
(?(1)\1) # if Group 1 is set, match Group 1
) Comment: start Group 1
:the literal text :
\d+a digit, repeated at least once
Comment: match a colon and some digits
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: end Group 1
Comment: end lookahead
Comment: end counter-line-skipper: zero or more times
.*?any character, repeated any number of times
Comment: match
\Kresets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
Comment: drop everything we've matched so far
pig the literal text pig (case sensitive) Comment: match pig (this is the match!)
Positive Lookahead(?=.*(\d+)(?(1)\1)) .*any character, repeated any number of times
\d+a digit, repeated at least once
Uses the first branch if group 1 matched
If the condition is true, match this branch:\1 \1the text saved by group 1
Comment: capture the next number to Group 2