import re
regex = re.compile(r"^ ([\w-_ ]+):\n\n +Type: Ethernet\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$", flags=re.MULTILINE)
test_str = ("ETHERNET CONNECTED, NO WI-FI\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
"Below Source Text from Mac Shell Script:\n"
"system_profiler SPNetworkDataType\n"
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
"Network:\n\n"
" Thunderbolt Ethernet Slot 1:\n\n"
" Type: Ethernet\n"
" Hardware: Ethernet\n"
" BSD Device Name: en8\n"
" IPv4 Addresses: 192.168.1.15\n"
" IPv4:\n"
" AdditionalRoutes:\n"
" DestinationAddress: 192.168.1.15\n"
" SubnetMask: 255.255.255.255\n"
" DestinationAddress: 169.254.0.0\n"
" SubnetMask: 255.255.0.0\n"
" Addresses: 192.168.1.15\n"
" ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69\n"
" ARPResolvedIPAddress: 192.168.1.1\n"
" Configuration Method: DHCP\n"
" ConfirmedInterfaceName: en8\n"
" Interface Name: en8\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: 00:50:b6:ca:f0:dc\n"
" Media Options: Full Duplex, Flow Control\n"
" Media Subtype: 1000baseT\n"
" Proxies:\n"
" Exceptions List: *.local, 169.254/16\n"
" FTP Passive Mode: Yes\n"
" Sleep Proxies:\n"
" 1 Family Room Apple TV:\n"
" Marginal Power: 60\n"
" Metric: 703560\n"
" Portability: 35\n"
" Total Power: 63\n"
" Type: 70\n"
" Service Order: 0\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"
" 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:\n"
" Configuration Method: DHCP\n"
" IPv6:\n"
" Configuration Method: Automatic\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\n"
"~~~~~~~~~~~~~~~~~~~~~~~~\n"
"Type: Ethernet\\n +Hardware.+\\n +BSD.+\\n +IPv4 Addresses: 192[\\.\\d]+$")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
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