import Foundation
let pattern = #"All files[^|]*\|[^|]*\s+([\d\.]+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
---------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------------|----------|----------|----------|----------|----------------|
[32;1mAll files [0m |[32;1m 85.61[0m |[33;1m 59.85[0m |[32;1m 84.06[0m |[32;1m 87.14[0m |[31;1m [0m |
[32;1m cli [0m |[32;1m 95.37[0m |[33;1m 70.21[0m |[33;1m 76.47[0m |[32;1m 97.12[0m |[31;1m [0m |
[32;1m add.js [0m |[32;1m 100[0m |[32;1m 83.33[0m |[32;1m 100[0m |[32;1m 100[0m |[33;1m 12[0m |
---------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------------|----------|----------|----------|----------|----------------|
All files | 85.61 | 59.85 | 84.06 | 87.14 | |
cli | 95.37 | 70.21 | 76.47 | 97.12 | |
add.js | 100 | 83.33 | 100 | 100 | 12 |
adduser.js | 100 | 83.33 | 100 | 100 | 21,23 |
new.js | 100 | 75 | 100 | 100 | 18,20 |
"""#
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