import Foundation
let pattern = #"^((qwarebatch[0-9]?_((g?p(tres|[fse])|lc|ra|darf?|n[edlcs]|ob|gru_intra))_)|([AG]F|[EL]?[CO]([ACDV])?|D[CIL]|NT))[0-9]{5,8}\.TXT.*"#
let regex = try! NSRegularExpression(pattern: pattern, options: [.anchorsMatchLines, .caseInsensitive])
let testString = #"""
qwarebatch_nc_20140714.TXT
AF32503.txt
CV4130214.TXT
CA5070515.TXT
CD4130115.txt
CC4130214.TXT
DI41102.txt
qwarebatch_dar_20150602.TXT
qwarebatch_darf_20150602.TXT
DC4130214.TXT
DL4160214.TXT
EC5070515.TXT
GF41102.txt
GP41102.TXT
qwarebatch_gru_intra_20150602.TXT
qwarebatch_lc_20150602.TXT
LO4130214.TXT
NT32503.txt
qwarebatch_nd_20150602.TXT
qwarebatch_ne_20150602.TXT
qwarebatch_nl_20150602.TXT
qwarebatch_ns_20150602.TXT
qwarebatch_ob_20150602.TXT
qwarebatch_pe_20140618.TXT
qwarebatch_pf_20150602.TXT
qwarebatch2_ptres_20150602.TXT
qwarebatch_ra_20150602.TXT
"""#
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