import re
regex = re.compile(r"^\s*D\d+\.node\s+\(([\d.]+)\):(?:(?!^\s*(?:D\d+\.node|Success rate is )\s).)*^\s*Success rate is 0\b", flags=re.MULTILINE | re.DOTALL)
test_str = (" Job Engine: Job engine1\n"
" \n"
" 05/05/2016 15:13:31 : Started PING ISDN : JobDescription_1defa6e9-1c83-4909-bc13-36d2e1205096\n"
" \n"
" Execute Command Script on Devices\n"
" Where (NCM_Group = 'ATM WAN Routers')\n"
" \n"
" D002.node (10.188.188.188):\n"
" \n"
" --------------\n"
" ping 10.191.231.254\n"
" --------------\n"
" Type escape sequence to abort.\n"
" Sending 5, 100-byte ICMP Echos to 10.91.31.254, timeout is 2 seconds:\n"
" .....\n"
" Success rate is 80 percent (4/5)\n"
" \n"
" ___________________________________________________________________________\n"
" D069.node (10.0.0.182):\n"
" \n"
" --------------\n"
" ping 10.191.231.254\n"
" --------------\n"
" Type escape sequence to abort.\n"
" Sending 5, 100-byte ICMP Echos to 10.91.31.254, timeout is 2 seconds:\n"
" ..!!!\n"
" Success rate is 60 percent (3/5), round-trip min/avg/max = 32/32/32 ms\n"
" \n"
" ___________________________________________________________________________\n"
" D069.node (10.0.0.183):\n"
" \n"
" --------------\n"
" ping 10.191.231.254\n"
" --------------\n"
" Type escape sequence to abort.\n"
" Sending 5, 100-byte ICMP Echos to 10.91.31.254, timeout is 2 seconds:\n"
" ..!!!\n"
" Success rate is 0 percent (0/5), round-trip min/avg/max = 32/32/32 ms\n"
" \n"
" ___________________________________________________________________________")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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