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