# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\[[\d\.]+\] (\d{2,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})[\,:\s\[\]\d{1,2}\w]+([\d\.]{4,7})+[\w\s\d\(\)\.,%]+"
test_str = ("traceroute to 10.20.5.111 (10.20.5.111), 30 hops max, 60 byte packets\n"
" 1 10.194.82.1 (10.194.82.1) 0.149 ms 0.139 ms 0.138 ms\n"
" 2 10.194.202.97 (10.194.202.97) 0.454 ms 0.499 ms 0.546 ms\n"
" 3 10.219.244.81 (10.219.244.81) 0.791 ms 0.792 ms 0.805 ms\n"
" 4 10.207.187.37 (10.207.187.37) 39.497 ms 39.489 ms 39.490 ms\n"
" 5 10.207.187.38 (10.207.187.38) 38.442 ms 39.839 ms 41.330 ms\n"
" 6 10.20.5.111 (10.20.5.111) 42.867 ms 43.448 ms 44.847 ms")
matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# 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