import Foundation
let pattern = #"Request\:\s\[(?P<req_ip>\d+\.\d+\.\d+\.\d+)"#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - Request: [192.168.1.88:54368] GET /:/timeline?ratingKey=68803&key=%2Flibrary%2Fmetadata%2F68803&state=paused&playQueueItemID=2831&time=1360507&duration=5694741 (34 live) TLS GZIP
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - Auth: We found auth token (xxxxxxxxxxxxxxxxxxxx), enabling token-based authentication.
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - Auth: Came in with a super-token, authorization succeeded.
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - Client [iqzupp52ref65urd69nng66r] reporting timeline state paused, progress of 1360507/5694741ms for guid=, ratingKey=68803 url=, key=/library/metadata/68803, containerKey=, metadataId=68803
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - Play progress on 68803 'Kung Fu Panda 3' - got played 1360507 ms by account 1!
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - [Now] User is sydvishus (ID: 1)
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - [Now] Device is Chrome (Plex Web (Chrome)).
Jun 20, 2016 17:59:39 [0x7f16b541b700] DEBUG - [Now] Updated play state for /library/metadata/68803.
Jun 20, 2016 17:59:39 [0x7f16c1bff700] DEBUG - Completed: [192.168.1.88:54368] GET /:/timeline?ratingKey=68803&key=%2Flibrary%2Fmetadata%2F68803&state=paused&playQueueItemID=2831&time=1360507&duration=5694741 (34 live) TLS GZIP 4ms 276 bytes 200 (pipelined: 20)
"""#
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