import Foundation
let pattern = #"(?P<ID>[A-Z0-9]{4})(?P<CATEGORY>0000[A-Z0-9]{1})(?P<TIMESTAMP>[0-9]{10})"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
D812E66E0000A13297539619F990000A1326011227538F0000A134114416231680000A1333737336D7AB0000A1367834926D1A90000A1340522218B4D40000A1339938392DD4C0000A1346574058C1A20000A134475357330810000A136293851756F20000A1329934748A71A0000A1383377560B3F90000A13460667155BBE0000A1370955439BF490000A1336724039A6E90000A132550239864440000A13827086085A3F0000A1353692293BB7C0000A136799576755110000A1327676142DA5B0000A13536535381E270000A1332164993941B0000A133802356236AF0000A1345444250FBC50000A134544825251590000A1355240920
"""#
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