import Foundation
let pattern = #"[a-z0-9 %\-\+:\/]*\b(\d{1,3}\.?\d{0,4})\.?[a-z0-9 \(\)]*out of[a-z ]*(\d{1,3}\.?\d{0,4})\.?.*"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
4 out of 6.0
2.979 out of 4 gpa
out of 5
gpa 3.712 out of 4
110 out of 110
4.94 (for second part) out of 6
9.45 points out of 10 (240 ects)
5.7 out of 7.
cgpa- 3.66 out of 4
a-/gpa :3.55 out of 4.00
4.5 out of a possible 5
a4 19 out of 22
1.15 1 being the best grade out of 5
3.64out of 6
28 out of 30. final grade 108 out of 110
2.64out of 4 average 74.02%
81.77. gpa:3.7out of 4.3
81.77gpa:3.7 out of 1.3
cumulative gpa 16.22out of 20.00
9.13 rank 20 out of 80
a+/9.65 out of 10
3.6/4 or 17.87 out of 20
87.1%/gpa 3.71 out of 5.0
"""#
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