import Foundation
let pattern = #"(?<date>\d[\d:.]+)\W+(?<InPorts>\d+):\s*(?<OutPort>\d+)\W+G-MST:\s*(?<GMST>\w+)\W+guid=(?<Guid>[^"]+)"\W+(?<SourceIP>\d{1,3}(?:\.\d{1,3}){3})\W+(?<targetIP>\d{1,3}(?:\.\d{1,3}){3})\W+(?<SourceSpeed>\d+)\W+(?<TargetSpeed>\d+)\D+(?<AudioType>\d+)\D+(?<rsn>\d+)\W+(?<utc>\d[\d.:]*)\D+(?<pl>\d+)\D+(?<s>\d+)\D+(?<r>\d+)\D+(?<l>\d+)\D+(?<j>\d+)\D+(?<u>\d+)\D+(?<o>\d+)\D+(?<flags>0x\d+).*?:(?<sip>[^"]*)"\D+(?<vpn>\d+)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"08:07:46.914 ( 1708: 8624) G-MST: 400000EF " guid=00040000-73b2-5c7f-2295-00104941e7b0" ("10.10.60.3","10.10.29.251"),(10292, 59046),2(ULaw),rsn:1,12:05:15.623 (UTC),pl:20,(s:7525, r:7557, l:0),(j:0,u:27037,o:0) flgs:0x00000000 "sip:TGrp_5,p111@10.10.60.3:5441",vpn:0"#
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