import Foundation
let pattern = #"(\d+.\d+) "([\w]+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
"Title: Y"
"Method: Mass-loaded (1.500 grams)"
"DCR mode: Measure (-0.07 ohms)"
"Area (Sd): 12.88 sq cm"
"QC-limits file: "
"Last setup file: C:\MLS\SPO-W.SET modified "
"Series resistance: 75.00 ohms"
"Stimulus level: 4.08 volts"
"SPLref reference impedance: NONE"
"Large units (volume = liters, mass = grams)"
0.169 "RMSE-free Ohms"
202.854 "Fs Hz"
3.473 "Re Ohms[dc]"
23.567 "Res Ohms"
6.487 "Qms "
0.956 "Qes "
0.833 "Qts "
0.065 "L1 mH"
0.058 "L2 mH"
0.827 "R2 Ohms"
0.130 "RMSE-load Ohms"
0.092 "Vas(Sd) liters"
1.551 "Mms grams"
396.893 "Cms �M/Newton"
2.680 "Bl Tesla-M"
80.900 "SPLref(Sd) dB "
0.043 "Rub-index "
"""#
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