import Foundation
let pattern = #"^(\d{2})\s(\d.\d)\s(\w+(?:-+\w+)*(?:--\w)?-?)\s((?:\d+,)?\d+.\d+)\s((?:\d+,)?\d+.\d+)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
69 1.0 PKS-EKC-FlYCTRK--Y 2,110.28 2,110.28
70 2.0 ACS-PMM 31.75 63.50
72 1.0 PKS-TR1-CRD 308.14 308.14
73 1.0 QTC-01HZZ-RBER058- 1,725.57 1,725.57
74 1.0 MGS-05B-4TC-120--8 1,222.84 1,222.84
75 1.0 ACS-VGY 98.60 98.60
76 2.0 ACS-VGG 27.27 54.53
77 2.0 ACS-VGQ 110.93 221.86
78 2.0 ECS-ENM--845 1,294.18 2,588.36
80 1.0 FREIGHT 4,188.00 4,188.00
"""#
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