import Foundation
let pattern = #"\* LIST \((?<Markers>.*)\) \"\/\" (?<folder>.*?)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
* LIST (\HasChildren) "/" INBOX/Arkabit
* LIST (\HasNoChildren) "/" "INBOX/Arkabit/Flora (SAP)"
* LIST (\HasNoChildren) "/" INBOX/BRW
* LIST (\HasNoChildren) "/" INBOX/Chiptronics
* LIST (\HasNoChildren) "/" INBOX/CRMB1.COM
* LIST (\Marked \HasChildren) "/" INBOX/CVA
* LIST (\HasNoChildren) "/" INBOX/CVA/Contabilista
* LIST (\HasNoChildren) "/" INBOX/CVA/Maxflex
* LIST (\HasNoChildren) "/" INBOX/Decorlux
* LIST (\HasNoChildren) "/" INBOX/Fabiano
* LIST (\HasNoChildren) "/" INBOX/Fabielle
* LIST (\HasNoChildren) "/" INBOX/Gamma
* LIST (\HasNoChildren) "/" INBOX/Integrate
* LIST (\HasNoChildren) "/" INBOX/Kelli
* LIST (\HasNoChildren) "/" INBOX/Laborclin
* LIST (\HasNoChildren) "/" INBOX/Macrosul
* LIST (\HasChildren) "/" INBOX/MadeiraMadeira
* LIST (\HasNoChildren) "/" INBOX/MadeiraMadeira/BRW
* LIST (\HasNoChildren) "/" INBOX/MadeiraMadeira/Eglo
* LIST (\HasNoChildren) "/" "INBOX/MadeiraMadeira/Full Fit"
* LIST (\HasNoChildren) "/" "INBOX/MadeiraMadeira/Glass Mosaic"
* LIST (\Marked \HasNoChildren) "/" INBOX/MadeiraMadeira/Luciin
* LIST (\HasNoChildren) "/" INBOX/MadeiraMadeira/Rojemac
* LIST (\HasNoChildren) "/" "INBOX/Mercado do Alarme"
* LIST (\Marked \HasChildren) "/" INBOX/Stuff
* LIST (\HasNoChildren) "/" INBOX/Stuff/Importado
* LIST (\Marked \HasNoChildren) "/" INBOX/Stuff/Ita&APo-
* LIST (\Marked \HasNoChildren) "/" INBOX/Stuff/Literal
* LIST (\HasChildren) "/" INBOX/Stuff/News
* LIST (\HasNoChildren) "/" INBOX/Stuff/News/Linkedin
* LIST (\HasNoChildren) "/" INBOX/Stuff/News/Microsoft
* LIST (\HasNoChildren) "/" INBOX/Stuff/News/SAP
* LIST (\Marked \HasNoChildren) "/" INBOX/Stuff/Postman
* LIST (\HasChildren) "/" INBOX/Stuff/PrimaERP
* LIST (\HasNoChildren) "/" INBOX/Stuff/PrimaERP/Faturas
* LIST (\Marked \HasNoChildren) "/" INBOX/Stuff/VSOnline
* LIST (\HasNoChildren) "/" "INBOX/Suporte | CRMSAP"
* LIST (\HasNoChildren) "/" INBOX/Umbler
$ OK LIST completed.
"""#
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