# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(address=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+) (mac-address=[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}:[0-9A-F]{2}) .*(host-name=.*)"
test_str = (" 0 address=172.31.10.254 mac-address=00:02:03:07:91:26 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=11h38m39s last-seen=12h21m21s active-address=172.31.10.254 active-mac-address=00:02:03:07:91:26 active-server=DHCP_CENTRALPACK host-name=Desenvol-vimento\n"
" 1 address=172.31.10.253 mac-address=00:18:4D:02:81:35 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=14h17m50s last-seen=9h42m10s active-address=172.31.10.253 active-mac-address=00:18:4D:02:81:35 active-server=DHCP_CENTRALPACK host-name=Estoque-bicolor\n"
" 2 address=172.31.10.252 mac-address=74:86:7A:F6:2A:16 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=22h41m28s last-seen=1h18m32s active-address=172.31.10.252 active-mac-address=74:86:7A:F6:2A:16 active-client-id=1:74:86:7a:f6:2a:16 active-server=DHCP_CENTRALPACK host-name=CTPMCFA23\n"
" 3 address=172.31.10.251 mac-address=D4:AE:52:FD:9C:B5 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h19m36s last-seen=30m14s active-address=172.31.10.251 active-mac-address=D4:AE:52:FD:9C:B5 active-client-id=1:d4:ae:52:fd:9c:b5 active-server=DHCP_CENTRALPACK host-name=PRTMC1344\n"
" 4 address=172.31.10.250 mac-address=9C:B6:54:3A:41:C3 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" last-seen=6w2d3h57m24s host-name=ctpimfa30\n"
" 5 address=172.31.10.248 mac-address=10:1F:74:3C:07:2C address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound last-seen=3h26m6s active-address=172.31.10.248 active-mac-address=10:1F:74:3C:07:2C active-client-id=1:10:1f:74:3c:7:2c active-server=DHCP_CENTRALPACK host-name=NPI3C072C\n"
" 6 address=172.31.10.247 mac-address=00:18:4D:02:81:34 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=16h12m2s last-seen=7h47m58s active-address=172.31.10.247 active-mac-address=00:18:4D:02:81:34 active-server=DHCP_CENTRALPACK host-name=estoque-flexo\n"
" 7 address=172.31.10.246 mac-address=00:1E:C9:30:00:74 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h20m50s last-seen=1h32m36s active-address=172.31.10.246 active-mac-address=00:1E:C9:30:00:74 active-client-id=1:0:1e:c9:30:0:74 active-server=DHCP_CENTRALPACK host-name=PRTMC131D\n"
" 8 address=172.31.10.245 mac-address=90:B1:1C:F7:0B:9D address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h50m6s last-seen=1h46m37s active-address=172.31.10.245 active-mac-address=90:B1:1C:F7:0B:9D active-client-id=1:90:b1:1c:f7:b:9d active-server=DHCP_CENTRALPACK host-name=CTPMCFA1C\n"
" 9 address=172.31.10.244 mac-address=00:19:BB:61:81:06 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h40m15s last-seen=1h18m41s active-address=172.31.10.244 active-mac-address=00:19:BB:61:81:06 active-client-id=1:0:19:bb:61:81:6 active-server=DHCP_CENTRALPACK host-name=prtmc1317\n"
"10 address=172.31.10.243 mac-address=74:86:7A:FC:E2:37 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" last-seen=9w3d1h11m31s host-name=PRTNB02FB\n"
"11 address=172.31.10.242 mac-address=A0:2B:B8:00:A0:7A address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h31m40s last-seen=13s active-address=172.31.10.242 active-mac-address=A0:2B:B8:00:A0:7A active-client-id=1:a0:2b:b8:0:a0:7a active-server=DHCP_CENTRALPACK host-name=CTPMCFA2B\n"
"12 address=172.31.10.241 mac-address=00:18:8B:E0:FD:54 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h50m25s last-seen=50m9s active-address=172.31.10.241 active-mac-address=00:18:8B:E0:FD:54 active-client-id=1:0:18:8b:e0:fd:54 active-server=DHCP_CENTRALPACK host-name=CTPMC1312\n"
"13 address=172.31.10.240 mac-address=D4:AE:52:FD:9C:B2 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=21h52m41s last-seen=43m10s active-address=172.31.10.240 active-mac-address=D4:AE:52:FD:9C:B2 active-client-id=1:d4:ae:52:fd:9c:b2 active-server=DHCP_CENTRALPACK host-name=PRTMC1346\n"
"14 address=172.31.10.239 mac-address=00:C0:EE:94:1E:0B address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=20h32m1s last-seen=3h27m59s active-address=172.31.10.239 active-mac-address=00:C0:EE:94:1E:0B active-client-id=1:0:c0:ee:94:1e:b active-server=DHCP_CENTRALPACK\n"
"15 address=172.31.10.238 mac-address=00:19:BB:61:71:A4 address-lists=\"\" server=DHCP_CENTRALPACK dhcp-option=\"\" status=bound expires-after=17h57m28s last-seen=55m1s active-address=172.31.10.238 active-mac-address=00:19:BB:61:71:A4 active-client-id=1:0:19:bb:61:71:a4 active-server=DHCP_CENTRALPACK host-name=CTPMC0F09\n"
"16 address=172.31.10.237 mac-address=90:B1:1C:F7:0A:39 address-lists=\"\" server=DHCP_CENTRALPACK always-broadcast=yes dhcp-option=\"\" status=bound expires-after=20h16m33s last-seen=22m40s active-address=172.31.10.237 active-mac-address=90:B1:1C:F7:0A:39 active-client-id=1:90:b1:1c:f7:a:39 active-server=DHCP_CENTRALPACK host-name=PRTMC0139\n")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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