import Foundation
let pattern = #"10\.(?:[0-9]|[0-1][0-9]?|20)\.0\.(?:25[0-4]|2[0-4][0-9]|1[0-9][0-9]|[2-9][0-9]|1[1-9]|[1-9])"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
10.0.0.1
10.19.0.13
10.22.0.13
10.20.0.2
10.0.0.6
10.24.0.1
10.19.0.233
10.18.0.13
10.17.0.12
10.0.0.12
10.0.0.211
10.3.0.123
10.4.0.121
10.5.0.200
10.5.0.201
10.5.0.1
10.4.0.0
10.4.0.254
10.0.0.12
10.9.0.134
10.10.0.112
10.20.0.254
10.20.0.1
10.20.0.9
10.19.0.19
10.18.0.18
10.13.0.1
"""#
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