import Foundation
let pattern = #"([0-9]+)\s+([-.a-zA-Z0-9]+)\s+(\(([-.a-zA-Z0-9]+)\))?(.*)$"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
1 10.110.255.129 42.759 ms 42.721 ms 42.718 ms
2 10.24.171.1 48.262 ms 48.628 ms 48.626 ms
3 10.110.255.186 43.377 ms 43.588 ms 43.602 ms
4 10.251.5.234 44.628 ms 44.625 ms 44.620 ms
5 10.251.5.162 163.039 ms 163.590 ms 163.581 ms
6 10.251.5.153 160.607 ms 157.800 ms 158.146 ms
7 10.3.63.132 181.292 ms 181.304 ms 181.171 ms
8 10.3.63.162 158.832 ms 162.948 ms 162.924 ms
9 10.3.64.239 156.895 ms 155.873 ms 156.368 ms
1 10.110.255.129 (10.110.255.129) 46.122 ms 46.160 ms 46.155 ms
2 jira1.cos.git.jdsu.net (10.24.171.1) 49.325 ms 49.308 ms 49.289 ms
3 10.110.255.186 (10.110.255.186) 47.280 ms 47.263 ms 47.245 ms
4 10.251.5.234 (10.251.5.234) 49.201 ms 49.184 ms 49.169 ms
5 10.251.5.162 (10.251.5.162) 181.687 ms 181.928 ms 181.910 ms
6 10.251.5.153 (10.251.5.153) 176.666 ms 169.844 ms 171.617 ms
7 10.3.63.132 (10.3.63.132) 172.793 ms 174.712 ms 174.690 ms
8 10.3.63.162 (10.3.63.162) 186.754 ms 187.607 ms 187.592 ms
9 10.3.64.239 (10.3.64.239) 173.878 ms 175.426 ms 175.500 ms
magic scenarios: :D (be hostname)
1 91.121.19.253 1.349 ms 1.343 ms 1.862 ms
2 37.187.231.105 0.649 ms 0.602 ms 37.187.231.95 0.739 ms
3 37.187.232.88 12.179 ms 37.187.232.96 12.365 ms 12.424 ms
4 80.81.194.118 42.681 ms 42.771 ms 42.915 ms
5 213.252.224.238 43.622 ms 43.458 ms 43.479 ms
6 84.15.6.230 43.573 ms 84.15.6.226 43.323 ms 213.226.129.50 40.188 ms
7 141.136.40.34 40.250 ms 213.226.129.50 40.059 ms 40.051 ms
magic scenarios: :D (su hostname)
1 rbx1-3b-a9.fr.eu (91.121.19.253) 1.648 ms 3.061 ms 3.220 ms
2 be11-315.rbx-g2-a9.fr.eu (37.187.231.107) 0.763 ms be11-315.rbx-g1-a9.fr.eu (37.187.231.105) 0.821 ms be11-146.rbx-g2-a9.fr.eu (37.187.231.97) 1.300 ms
3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.250 ms be100-1105.fra-1-a9.de.eu (37.187.232.88) 12.354 ms be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.293 ms
4 r2223.bee.lt (80.81.194.118) 42.643 ms 42.734 ms 42.870 ms
5 213.252.224.238 (213.252.224.238) 43.368 ms 43.399 ms 84.15.6.226 (84.15.6.226) 43.676 ms
6 84.15.6.226 (84.15.6.226) 43.460 ms 213.226.129.50 (213.226.129.50) 40.203 ms 84.15.6.226 (84.15.6.226) 43.329 ms
7 s1v3.e-lietuva.lt (141.136.40.34) 40.225 ms 213.226.129.50 (213.226.129.50) 40.106 ms 40.082 ms
1 rbx1-3b-a9.fr.eu (91.121.19.253) 1.648 ms 3.061 ms 3.220 ms
2 be11-315.rbx-g2-a9.fr.eu (37.187.231.107) 0.763 ms
2 be11-315.rbx-g1-a9.fr.eu (37.187.231.105) 0.821 ms
2 be11-146.rbx-g2-a9.fr.eu (37.187.231.97) 1.300 ms
3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.250 ms
3 be100-1105.fra-1-a9.de.eu (37.187.232.88) 12.354 ms
3 be100-1120.fra-5-a9.de.eu (37.187.232.96) 12.293 ms
4 r2223.bee.lt (80.81.194.118) 42.643 ms 42.734 ms 42.870 ms
5 213.252.224.238 (213.252.224.238) 43.368 ms 43.399 ms
5 84.15.6.226 (84.15.6.226) 43.676 ms
6 84.15.6.226 (84.15.6.226) 43.460 ms
6 213.226.129.50 (213.226.129.50) 40.203 ms
6 84.15.6.226 (84.15.6.226) 43.329 ms
7 s1v3.e-lietuva.lt (141.136.40.34) 40.225 ms
7 213.226.129.50 (213.226.129.50) 40.106 ms 40.082 ms
"""#
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