import Foundation
let pattern = #"^[\dA-F]+[ \t]+[\dA-F]+(?: [\dA-F]+)*[ \t]+"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
0 4D1F 8172 DC.L $4D1F8172 ; Rom CheckSum
4 0040 002A DC.L $0040002A ; Boot Vector = EBootStart
8 00 DC.B $00 ; Machine Type
9 75 DC.B $75 ; Rom Version
A 6000 0056 Bra L3
E 6000 0750 Bra L62
12 6000 0044 Bra L2
16 6000 0016 Bra E_6
1A 0001 76F8 DC.L $000176F8 ; offset of Resources in ROM
1E 4EFA 2BFC Jmp P_mvDoEject
22 0000 0000 DC.L $00000000
26 0000 0000 DC.L $00000000
1FFE2 4B57 4B20 4C41 DC.B 'KWK LA'
"""#
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