import Foundation
let pattern = #"((\+|\+\s|\d{1}\s?|\()(\d\)?\s?[-\.\s\(]??){8,}\d{1}|\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
+61 2 8249 5000
+61 2 6122 5100
+61 2-9407 5247
+65-8233-6124
+61 412 364 923
1300 890 902
0435 569 072
8801911384785
0086-010-58732690
+ 852 2118 3893
(852) 2513 3168
(852) 2865-9898
+91-9552503220
+914042024714
+919948298078
+91 22 25094755
0124 4742300
+1 (972)273 0738
08111 90 1859
+62-21-5201214
+62 21 2995 1641
603-4270 2309
+603 7724 1199
+603-77108288
+632 8937377
(65) 6723 6888
+6596233196
+656379 2681
+65 6737 0324
82-70-7791-7100
+82 17 238 1226
82-31-478-4506
+822-6309-6366
(000)000-0000
(000)000 0000
(000)000.0000
(000) 000-0000
(000) 000 0000
(000) 000.0000
0000000000
(000)0000000
(000)000-0000
(000)000 0000
(000)000.0000
(000) 000-0000
(000) 000 0000
(000) 000.0000
000-0000
000 0000
000.0000
0000000
0000000000
(000)0000000
"""#
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