import Foundation
let pattern = #"\b(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\b"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
67.93.195.181
248.83.55.126
277.92.425.12
220.164.237.203
36.173.112.111
36.33.171.246
37.123.14.323
23.93.163.86
236.129.173.89
2.224.237.202
256.123.123.123
69.10.174.79
35.185.155.175
6.215.157.157
123.123.123.234
188.43.121.96
101.22.3.71
1.1.1.1
55.58.142.177
179.107.91.118
"""#
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