import Foundation
let pattern = #"(?m)^(?!0)(?=\d{10}$)(?=.*?(.)(?:.*\1){1,2})(?!.*?(.)(?:.*\2){3})(?!.*?(?!\1)(.).*\3).*$"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
1234567890
1234537890
1134567890
2123456789
1234267890
1671812342
1934466892:6
1234@56789
3563893124
4412356789
2349780915
981651321182234556
1234567890
10000000000
02346ex789
0000%00000
0234567 894
8232527884
02325278847
02345678945
2323456789
8152228374
0234527834
8183746528
0435465768
3245657689
"""#
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