import Foundation
let pattern = #"(?<=,|\t)(.*?)(?=,|\n)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
marjorie.bienert@soprasteria.com,adel.amari@ext.soprasteria.com,marcos.martinez@soprasteria.com,angeline.marie@soprasteria.com,frederic.lecomte2@soprasteria.com,stephane.gaston@sante.gouv.fr,juan-cristobal.peinado@soprasteria.com,philippe.bourdin@sante.gouv.fr,alejandro-horacio.ramos@soprasteria.com,thomas.bernstein@soprasteria.com,iman.ouazzani@soprasteria.com,anthony.jacq@soprasteria.com,jorge.moraistejerina@soprasteria.com,camille.rolland@soprasteria.com,sebastien.dufay@soprasteria.com,soufiane.mouflih@ext.soprasteria.com,adama.sakho@soprasteria.com,deuneuv.makoundou@soprasteria.com,manel.bejaoui@soprasteria.com,etienne.lorteau@soprasteria.com,javier.pina@soprasteria.com,paloma.fernandez@soprasteria.com,victoire.legrand@soprasteria.com,timothee.luyt@soprasteria.com,francisco-javier.soriano@soprasteria.com,christophe.lauv@soprasteria.com,thibault.louis@soprasteria.com,yolanda.torres@soprasteria.com
admin.platines@asipsante.fr E A noreply.platines@asipsante.fr
"""#
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