import Foundation
let pattern = #"(FAIL|OK)$"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
2019-08-02 07:33:32 ---> OK
2019-08-02 08:32:42 ---> FAIL
2019-08-02 09:29:31 ---> OK
2019-08-02 10:29:23 ---> FAIL
2019-08-02 11:31:45 ---> OK
2019-08-02 12:30:55 ---> OK
2019-08-02 13:29:14 ---> FAIL
2019-08-02 14:29:19 ---> OK
2019-08-02 15:31:28 ---> OK
2019-08-02 16:28:39 ---> OK
2019-08-02 17:27:46 ---> OK
2019-08-03 05:28:31 ---> FAIL
2019-08-05 10:27:37 ---> OK
2019-08-05 11:26:39 ---> OK
2019-08-03 05:28:31 ---> FAIL
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
if let firstMatch = regex.firstMatch(in: testString, range: stringRange) {
let result: [String] = (1 ..< firstMatch.numberOfRanges).map { (testString as NSString).substring(with: firstMatch.range(at: $0)) }
print(result)
} else {
print("No matches were found.")
}
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