import Foundation
let pattern = #"^ ([\w-_ ]+):\n\n +Type: AirPort\n +Hardware.+\n +BSD Device Name: (.+)\n +IPv4 Addresses: (192[\.\d]+)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
ETHERNET DISCONNECTED, WI-FI RUNNING
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Network:
Thunderbolt Ethernet Slot 1:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en8
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Ethernet:
MAC Address: 00:50:b6:ca:f0:dc
Media Options:
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 0
Thunderbolt Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en6
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 1
Wi-Fi:
Type: AirPort
Hardware: AirPort
BSD Device Name: en0
IPv4 Addresses: 192.168.1.16
IPv4:
AdditionalRoutes:
DestinationAddress: 192.168.1.16
SubnetMask: 255.255.255.255
DestinationAddress: 169.254.0.0
SubnetMask: 255.255.0.0
Addresses: 192.168.1.16
ARPResolvedHardwareAddress: 2c:b0:5d:25:af:69
ARPResolvedIPAddress: 192.168.1.1
Configuration Method: DHCP
ConfirmedInterfaceName: en0
Interface Name: en0
Network Signature: IPv4.Router=192.168.1.1;IPv4.RouterHardwareAddress=2c:b0:5d:25:af:69
Router: 192.168.1.1
Subnet Masks: 255.255.255.0
IPv6:
Configuration Method: Automatic
DNS:
Server Addresses: 192.168.1.1
DHCP Server Responses:
Domain Name Servers: 192.168.1.1
Lease Duration (seconds): 0
DHCP Message Type: 0x05
Routers: 192.168.1.1
Server Identifier: 192.168.1.1
Subnet Mask: 255.255.255.0
Ethernet:
MAC Address: 28:cf:e9:17:e0:d1
Media Options:
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 2
USB Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en3
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 3
AX88179 USB 3.0 to Gigabit Ethernet:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en7
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 4
Thunderbolt FireWire:
Type: FireWire
Hardware: FireWire
BSD Device Name: fw0
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Ethernet:
MAC Address: 00:50:b6:10:00:00:5d:31
Media Options: Full Duplex
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 5
iPhone:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en5
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 6
Bluetooth PAN:
Type: Ethernet
Hardware: Ethernet
BSD Device Name: en4
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 7
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let matches = regex.matches(in: testString, range: stringRange)
var result: [[String]] = []
for match in matches {
var groups: [String] = []
for rangeIndex in 1 ..< match.numberOfRanges {
let nsRange = match.range(at: rangeIndex)
guard !NSEqualRanges(nsRange, NSMakeRange(NSNotFound, 0)) else { continue }
let string = (testString as NSString).substring(with: nsRange)
groups.append(string)
}
if !groups.isEmpty {
result.append(groups)
}
}
print(result)
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 Swift 5.2, please visit: https://developer.apple.com/documentation/foundation/nsregularexpression