import Foundation
let pattern = #"<NumDI>(?'num_externe'.*)<\x2FNumDI>.*<NomDemandeur>(?'apt_nom'.*)<\x2FNomDemandeur>.*<Installation>(?'ins_libl'.*)<\x2FInstallation>.*<Localisation>(?'apt_localisation'.*)<\x2FLocalisation>.*<Symptome>(?'tsy_lib'.*)<\x2FSymptome>.*<SousSymptome>(?'qsy_lib'.*)<\x2FSousSymptome>.*<DateSouhaitee>(?'dtheure_limite'.*)<\x2FDateSouhaitee>.*<Commentaires>(?'commentaire_externe'.*)<\x2FCommentaires>"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"<DEMANDE> <NumDI> 247902</NumDI> <NomDemandeur>BOURG</NomDemandeur> <Installation>84CA21558</Installation> <Localisation>ZAC,,,</Localisation> <Symptome>CHA2</Symptome> <SousSymptome>CHA2</SousSymptome> <Urgence>1</Urgence> <DateSouhaitee>15/05/2019</DateSouhaitee> <Commentaires>TEST DE CREATION DE DEMANDE</Commentaires> </DEMANDE> "#
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