import Foundation
let pattern = #"^((0|1)[0-9])\/([0-3][0-9])\/([0-9][0-9])"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
01/01/20
02/01/20
03/01/20
04/01/20
05/01/20
06/01/20
07/01/20
08/01/20
09/01/20
10/01/20
11/01/20
12/01/20
01/32/20
01/03/20
01/04/20
01/05/20
01/06/20
01/07/20
01/08/20
01/09/20
01/10/20
01/11/20
01/12/20
01/13/20
01/14/20
01/15/20
01/16/20
01/17/20
01/18/20
01/19/20
01/20/20
01/21/20
01/22/20
01/23/20
01/24/20
01/25/20
01/26/20
01/27/20
01/28/20
01/29/20
01/30/20
01/31/20
"""#
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