import Foundation
let pattern = #"(?<type>[A-Za-z]{1})(?<subtype>[A-Za-z<]{1})(?<expcountry>[A-Za-z]{3})(?<docnumber>[0-9A-Za-z<]{9})(?<docnumbercheck>[0-9]{1})(?<complement>[0-9a-zA-Z<]{15})\n(?<birthdate>[0-9]{6})(?<birthcheck>[0-9]{1})(?<sex>[mfMF]{1})(?<expiredate>[0-9]{6})(?<expirecheck>[0-9]{1})(?<nacionality>[A-Za-z]{3})(?<optional>[a-zA-Z0-9<]{11})(?<linecheck>[0-9]{1})\n(?<lname>[A-Z]+)(?<lname2><[A-Z]+){0,}<<(?<spacing>[<]{0,})(?<fname>[A-Z]+)(?<mname1><[A-Z]+){0,}(?<namecomplement>[a-zA-Z<]+){0,}"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
IDCHES0002068<8<<<<<<<<<<<<<<<
8102287F1301014CHE<<<<<<<<<<<4
VADIS<<QUO<<<<<<<<<<<<<<<<<<<<
I<UTOD231458907<<<<<<<<<<<<<<<
7408122F1204159UTO<<<<<<<<<<<6
ERIKSSON<<ANNA<MARIA<<<<<<<<<<
I<NLDXI020DF236123456783<<<<<<
7208148F1108268NLD<<<<<<<<<<<9
VAN<DER<STEEN<<MARIANNE<LOUISE
"""#
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