import re
regex = re.compile((r"(?xsm) # free-spacing mode, multi-line\n"
r"(?=.*?pig) # fail right away if pig isn't there\n"
r"(?=\n"
r"( # Group 1\n"
r" (?: # skip one line that doesn't have pig\n"
r" ^ # beginning of line\n"
r" (?:(?!pig)[^\r\n])* # zero or more chars not followed by pig\n"
r" (?:\r?\n) # newline chars\n"
r" ) \n"
r" (?:(?1)|[^:]+) # recurse Group 1 OR match all chars that are not a :\n"
r" (:\d+) # match a sequence of digits\n"
r")? # End Group 1\n"
r") # End lookahead. Group 1, if set, now contains the number of lines skipped\n"
r".*?\Kpig # match pig\n"
r"(?=.*?(?(2)\2):(\d+))"))
test_str = ("my cat pi g\n"
"dog pi g\n"
"my pig\n"
"my cow\n"
"my mouse\n\n"
":1:2:3:4:5:6:7")
subst = "\\3"
result = regex.sub(subst, test_str, count=1)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html