import Foundation
let pattern = ##"^\$\$(?<uid>\d{9})\,(?<ev>\d{4})\,(?<d>\d{2}\/\d{2}\/\d{4})?\,(?<t>\d{2}\:\d{2}\:\d{2})\,(?<lt>\-?\d{1,3}\.?\d{1,6})\,(?<ln>\-?\d{1,3}\.?\d{1,6})\,(?<al>\-?\d{1,7})\,(?<sp>\d{1,3}\.?\d{0,3})\,(?<hd>\d{1,4})\,(?<sv>\d{1,2})\,(?<hp>\d{1,3}\.?\d{1,2})\,(?<bv>\d{1,5}\.?\d{1,4})\,(?<cq>\d{1,2})\,(?<mi>\d*)\,(?<gs>[012345]{1})\,(?<gt>\d{1,5}\.?\d?)\,(?<ac>\-?\d{1,3}\.?\d{0,3})\,(?<dc>\-?\d{1,4}\.?\d{0,3})\,(?<in1s>[01]{1})\,(?<in1a>\d*)\,(?<in2s>[01])\,(?<ig>[012])\#\#"##
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = ###"$$183900057,4002,03/05/2019,22:23:57,33.093139,-97.132556,2213,0.0,0,9,2.3,13.967,27,0,3,0,0.0,0.0,0,0,0,2##"###
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