import Foundation
let pattern = #"(\d[ \-.)]{0,3}){11,20}|(\b[\d]{3}\b)[^,.]"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
1546
4 8 6 2 6 4 1 1 5 3 6 6 6 7 7 1
5378 2052 1163 1153
6011-7614 4775 1364
333 112,55 mdsksajfk
05536080
214
12/05
12/08
122sss 222222222222111 22464 222 222ss, 222,22 22.22 333215122 22 33 123456789012
122.44
445
1546
4 8 6 2 6 4 1 1 5 3 6 6 6 7 7 1
5378 2052 1163 1153
6011-7614 4775 1364
333 112,55 mdsksajfk
11947285580
(11)947285580
1194728-5580
11 94728 5580
346.549.668-32
46254966832
31604523875
10154511,55
12/56/12019
"""#
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