import Foundation
let pattern = #"(\d){1}\t\t(\d){1,3}\%\s\t(\d){1}\t(\d){1,3}\%"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
Current car character
Power Handling Acceleration
102 107 104
Car character points history
Car part Level Action Wear New level New wear
Chassis: 6 96% 6 96%
Engine: 7 100% 7 100%
Front wing: 8 70% 8 70%
Rear wing: 8 75% 8 75%
Underbody: 8 63% 8 63%
Sidepods: 8 91% 8 91%
Cooling: 8 80% 8 80%
Gearbox: 7 82% 7 82%
Brakes: 9 25% 9 25%
Suspension: 8 94% 8 94%
Electronics: 8 78% 8 78%
(\d){1}\t\t(\d){1,3}\%\s\t(\d){1}\t(\d){1,3}\%
"""#
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