# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?s).*?(\z|\d+\.\d+\.\d+\.\d+)"
test_str = ("Nmap scan report for 192.168.0.21\n"
"Host is up (0.18s latency).\n"
"Not shown: 1 filtered port\n"
"Some closed ports may be reported as filtered due to --defeat-rst-ratelimit\n"
"PORT STATE SERVICE\n"
"22/tcp open ssh\n\n"
"Nmap scan report for 192.168.0.29\n"
"Host is up (0.029s latency).\n"
"Not shown: 1 filtered port\n"
"Some closed ports may be reported as filtered due to --defeat-rst-ratelimit\n"
"PORT STATE SERVICE\n"
"22/tcp open ssh\n\n"
"Nmap scan report for 192.168.0.25\n"
"Host is up (0.15s latency).\n"
"Not shown: 1 filtered port\n"
"Some closed ports may be reported as filtered due to --defeat-rst-ratelimit\n"
"PORT STATE SERVICE\n"
"22/tcp open ssh")
subst = "$1\\n"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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