# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"address\s*\d+\s*range\s(?<srcIp1>\S+)\s*(?<srcIp2>\S+)"
test_str = ("[USG6300E]display current-configuration\n"
"#\n"
"ip service-set TCP-TSGZ type object 514\n"
" service 0 protocol tcp source-port 8801 to 8804 destination-port 0 to 65535\n"
" service 1 protocol tcp source-port 12404 destination-port 0 to 65535\n"
"#\n"
"ip service-set GWDK type object 515\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 135\n"
" service 1 protocol tcp source-port 0 to 65535 destination-port 137 to 139\n"
" service 2 protocol tcp source-port 0 to 65535 destination-port 445\n"
" service 3 protocol tcp source-port 0 to 65535 destination-port 3389\n"
" service 4 protocol udp source-port 0 to 65535 destination-port 135\n"
" service 5 protocol udp source-port 0 to 65535 destination-port 137 to 139\n"
" service 6 protocol udp source-port 0 to 65535 destination-port 445\n"
" service 7 protocol udp source-port 0 to 65535 destination-port 3389\n"
"#\n"
"ip service-set FXTCP2403 type object 516\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 2403\n"
"#\n"
"ip service-set FXTCP2404 type object 517\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 2404\n"
"#\n"
"ip service-set FXTCPTSGZ type object 518\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 8801 to 8804\n"
" service 1 protocol tcp source-port 0 to 65535 destination-port 12404\n"
"#\n"
"ip service-set TCP5149 type object 519\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 5149 to 5150\n"
"#\n"
"ip service-set FXTCP5149 type object 520\n"
" service 0 protocol tcp source-port 5149 to 5150 destination-port 0 to 65535\n"
"#\n"
"ip service-set syslo type object 521\n"
" service 0 protocol udp source-port 0 to 65535 destination-port 162\n"
" service 1 protocol udp source-port 0 to 65535 destination-port 514\n"
"#\n"
"ip service-set UDPCJ type object 522\n"
" service 0 protocol udp source-port 0 to 65535 destination-port 161\n"
"#\n"
"ip service-set TCP102 type object 523\n"
" service 0 protocol tcp source-port 0 to 65535 destination-port 102\n"
" service 1 protocol tcp source-port 102 destination-port 0 to 65535\n"
"#\n"
"ip service-set serviceg1 type group 0\n"
" service 0 service-set ntp\n"
"#\n"
"ip service-set serviceg2 type group 1\n"
" service 0 service-set serviceg1\n"
" service 1 service-set FXTCPTSGZ\n"
"#\n\n"
"#\n"
"ip address-set TSGZZhu type object\n"
" address 0 range 10.78.48.112 10.78.48.118\n"
"#\n"
"ip address-set TSGZzi type object\n"
" address 0 10.82.186.11 mask 32\n"
"#\n"
"ip address-set JLzhu type object\n"
" address 0 range 10.78.44.1 10.78.44.15\n"
"#\n"
"ip address-set JLzi type object\n"
" address 0 range 10.82.186.5 10.82.186.6\n"
"#\n"
"ip address-set BXzhu type object\n"
" address 0 range 10.78.48.197 10.78.48.200\n"
"#\n"
"ip address-set BXzi type object\n"
" address 0 range 10.82.186.9 10.82.186.10\n"
"#\n"
"ip address-set DICPzhu type object\n"
" address 0 range 10.78.48.39 10.78.48.40\n"
"#\n"
"ip address-set DICPzi type object\n"
" address 0 10.82.186.4 mask 32\n"
"#\n"
"ip address-set erqu type object\n"
" address 0 10.85.186.0 mask 27\n"
"#\n"
"ip address-set addressg1 type group\n"
" address 0 address-set erqu\n"
"#\n"
"ip address-set addressg2 type group\n"
" address 0 address-set addressg1\n"
" address 1 address-set DICPzi\n"
"#\n")
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