import Foundation
let pattern = #"\d{8}[A-Z]\d{5}"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
123456789
987654321
987654322
987654323
98765432A12312
987654325
987654326
987654327
987654328
987654329
666218502
773218502
772218502
443218502
641218502
525.52.6969B
515-54-6976-A
555516316S
123.45.6789
987.65.4321
987.65.4322
987.65.4323
987.65.4324
987.65.4325
987.65.4326
987.65.4327
987.65.4328
987.65.4329
666.21.8502
773.21.8502
772.21.8502
772-21-8502
772218502
443.21.8502
641.21.8502
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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