import Foundation
let pattern = #"(\()?(\+36|0036|06)?(\))?(-| )?(1|20|2[2-9]|3[0-7]|40|42|4[4-9]|5[2-7]|59|60|62|63|66|68|69|70|7[2-9]|80|8[2-5]|8[7-9]]|90|9[2-9])([\\\/ ])?(\d{6,7}|\d{3}(-| )\d{3,4}|\d{3,4}(-| )\d{3})"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
205032935
305032935
705032935
06303335554
+36205032935
0036205032935
(06)-30\3333333
(06)-30\3333-333
(06)-303333333
(+36)303333333
(+36)-30\333 3333
(+36)-30/3333333
(0036)-30\333-3333
(0036)-30\3333 333
(0036) 303333333
0036 74 555 556
+3674 555556
0674555 556
(06)74555556
(0036)-74/555-556
"""#
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