import Foundation
let pattern = #"(\d{6}\_\d{9})\=.*(gurlan,danu)"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
[7384] [Birthdays]
[7384] 180528_201747263=198202141300
[7384] 180528_210749326=19840222
[7384] 180528_211723352=19831207
[7384] 180528_211845500=201607072350
[7384] 180528_212024116=19591231
[7384] 180528_212038465=19590323
[7384] 180528_213357273=19590811
[7384] 180528_213701342=19780608
[7384]
[7384] [NameSurname]
[7384] 180528_201747263=Gurlan V. Vasile aka. Licuta
[7384] 180528_210749326=Gurlan (Enea) V. Maria aka Mariana
[7384] 180528_211723352=Danu (Gurlan) F. Oana Nicoleta aka. Nikol
[7384] 180528_211845500=Gurlan V. Rebecca-Ioana
[7384] 180528_212024116=Gurlan P. Vasile
[7384] 180528_212038465=Nucu (Gurlan) N. Maria
[7384] 180528_213357273=Bogdan (Danu) G. Elena
[7384] 180528_213701342=Enea D. Ioan Dorin
"""#
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