import Foundation
let pattern = #"overdraftlog-[0-9]{14}.[0-9]{3}(.log$|.log.sha$)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
overdraft-transactions_2020-06-19_10_20200620020000.csv
overdraft-transactions_2020-06-19_10_20200620020000.csv.sha1
overdraft-transactions_2020-06-20_6_20200621020000.csv
overdraft-transactions_2020-06-20_6_20200621020000.csv.sha1
overdraft-transactions_2020-06-21_6_20200622020000.csv
overdraft-transactions_2020-06-21_6_20200622020000.csv.sha1
overdraft-transactions_2020-06-22_7_20200623020000.csv
overdraft-transactions_2020-06-22_7_20200623020000.csv.sha1
overdraft-transactions_2020-06-23_6_20200624020001.csv
overdraft-transactions_2020-06-23_6_20200624020001.csv.sha1
overdraft-application_2020-06-19_0_20200620020000.csv
overdraft-application_2020-06-19_0_20200620020000.csv.sha1
overdraft-application_2020-06-20_0_20200621020000.csv
overdraft-application_2020-06-20_0_20200621020000.csv.sha1
overdraft-application_2020-06-21_0_20200622020000.csv
overdraft-application_2020-06-21_0_20200622020000.csv.sha1
overdraft-application_2020-06-22_0_20200623020000.csv
overdraft-application_2020-06-22_0_20200623020000.csv.sha1
overdraft-application_2020-06-23_0_20200624020001.csv
overdraft-application_2020-06-23_0_20200624020001.csv.sha1
overdraftlog-20201005122521.748.log
overdraftlog-20201005122521.748.log.sha
overdraftlog-20201006030000.749.log
overdraftlog-20201006030000.749.log.sha
"""#
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