import Foundation
let pattern = #"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
11-D6-D4-D7-B0-80
B5-42-A1-A7-33-B1
8C-03-74-2B-C6-D1
B5-5E-20-CH-1D-E6
7F-83-86-B6-7A-93
31-CE-78-13-75-FF
E5-EF-62-80-46-BC
D8-G2-44-10-49-E8
C6-45-E2-C5-D8-B3
F2-CA-6C-77-C3-7D
05-A0-4F-8A-F9-71
82-7B-DZ-D5-06-DA
B8-9F-45-90-D9-B3
82-7B-D6-D5-06-DA
"""#
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