import Foundation
let pattern = #"([\d\s:,.-]+)\s+\w+\s+\w+QuoteHandler:(\w+)\s+Processed\s+(\d+)\s+quote\s+messages\s+for\s+service\s+([\w\s]+)\s+seq\sno:\s+(\d+)\s+Latency:\s+([-\d\.]+)\s+ms\s+exchange time:\s+([\d+\-\:\.\s]+)msg rate:\s+([\d\.]+)\s+msgs\/sec\s+Packets\s+on\s+client\s+queue:\s+(\d+),\s+packets\s+on\s+client\s+buffer:\s+(\d+),\s+packets\s+on\s+server\s+queue:\s+(\d+),\s+server\s+latency:\s+([\d\.]+)\s+ms,\s+round\s+trip\s+time:\s+([\d\.]+)\s+ms,\s+\w+\s+cnt:\s+(\d+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"04:46:03.318 INFO SprywareDirectFeedQuoteHandler:secondary Processed 136726 quote messages for service NSDQ Direct Feed seq no: 835534 Latency: 1 ms exchange time: 2016-05-17 04:46:03.317 msg rate: 25.157 msgs/sec Packets on client queue: 0, packets on client buffer: 0, packets on server queue: 0, server latency: 0.032 ms, round trip time: 0.103 ms, quote cnt: 136725"#
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