# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^ ([\w-_ ]+):\n\n +Type: AirPort\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$"
test_str = ("ETHERNET DISCONNECTED, WI-FI RUNNING\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n\n"
"Network:\n\n"
" Thunderbolt Ethernet Slot 1:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en8\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Ethernet:\n"
" MAC Address: 00:50:b6:ca:f0:dc\n"
" Media Options: \n"
" Media Subtype: Auto Select\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 0\n\n"
" Thunderbolt Ethernet:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en6\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 1\n\n"
" Wi-Fi:\n\n"
" Type: AirPort\n"
" Hardware: AirPort\n"
" BSD Device Name: en0\n"
" IPv4 Addresses: 192.168.1.16\n"
" IPv4:\n"
" AdditionalRoutes:\n"
" DestinationAddress: 192.168.1.16\n"
" SubnetMask: 255.255.255.255\n"
" DestinationAddress: 169.254.0.0\n"
" SubnetMask: 255.255.0.0\n"
" Addresses: 192.168.1.16\n"
" ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69\n"
" ARPResolvedIPAddress: 192.168.1.1\n"
" Configuration Method: DHCP\n"
" ConfirmedInterfaceName: en0\n"
" Interface Name: en0\n"
" Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69\n"
" Router: 192.168.1.1\n"
" Subnet Masks: 255.255.255.0\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" DNS:\n"
" Server Addresses: 192.168.1.1\n"
" DHCP Server Responses:\n"
" Domain Name Servers: 192.168.1.1\n"
" Lease Duration (seconds): 0\n"
" DHCP Message Type: 0x05\n"
" Routers: 192.168.1.1\n"
" Server Identifier: 192.168.1.1\n"
" Subnet Mask: 255.255.255.0\n"
" Ethernet:\n"
" MAC Address: 28:cf:e9:17:e0:d1\n"
" Media Options: \n"
" Media Subtype: Auto Select\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 2\n\n"
" USB Ethernet:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en3\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 3\n\n"
" AX88179 USB 3.0 to Gigabit Ethernet:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en7\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 4\n\n"
" Thunderbolt FireWire:\n\n"
" Type: FireWire\n"
" Hardware: FireWire\n"
" BSD Device Name: fw0\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Ethernet:\n"
" MAC Address: 00:50:b6:10:00:00:5d:31\n"
" Media Options: Full Duplex\n"
" Media Subtype: Auto Select\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 5\n\n"
" iPhone:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en5\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 6\n\n"
" Bluetooth PAN:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en4\n"
" IPv4:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Service Order: 7\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