(?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: fail right away if pig isn't there
Positive Lookahead(?=
( # Group 1
(?: # 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
)
(?:(?1)|[^:]+) # recurse Group 1 OR match all chars that are not a :
(:\d+) # match a sequence of digits
)? # End Group 1
) Group 1( # Group 1
(?: # 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
)
(?:(?1)|[^:]+) # recurse Group 1 OR match all chars that are not a :
(:\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 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
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 : (5810 / 728 / 3A16)
Comment: recurse Group 1 OR match all chars that are not a :
:the literal : (5810 / 728 / 3A16)
\d+a digit, repeated at least once
Comment: match a sequence of digits
Comment: End Group 1
Comment: End lookahead. Group 1, if set, now contains the number of lines skipped
.*?any character, repeated any number of times
\Kresets the starting point of the reported match. Any previously consumed characters are no longer included in the final match
pig the literal text pig (case sensitive) Comment: match pig
Positive Lookahead(?=.*?(?(2)\2):(\d+)) .*?any character, repeated any number of times
Uses the first branch if group 2 matched
If the condition is true, match this branch:\2 \2the text saved by group 2
:the literal : (5810 / 728 / 3A16)
\d+a digit, repeated at least once