import Foundation
let pattern = #"\S+\s\d+\s_HOS_EXTENDED_\s(\d+\.\d+\.\d+\.\d+)\s-\s-\s\[\S+\s\S+\]\s\"(?:GET|POST)\s\S+url=http[s]?\S+\.\S+=\S+(?:SELECT|CASE|WHEN|THEN)\S+\sHTTP\/\d+\.\d+\""#
let regex = try! NSRegularExpression(pattern: pattern)
let testString = #"""
dedi10.jnb2.host-h.net 0 _HOS_EXTENDED_ 129.232.136.66 - - [14/May/2019:09:35:07 +0200] "GET /wp-json/oembed/1.0/embed?url=https%3A%2F%2Fwww.kdomainname%2F&format=xml%27%29%20AND%207814%3DCONVERT%28INT%2C%28SELECT%20CHAR%28113%29%2BCHAR%28106%29%2BCHAR%28112%29%2BCHAR%28120%29%2BCHAR%28113%29%2B%28SELECT%20%28CASE%20WHEN%20%287814%3D7814%29%20THEN%20CHAR%2849%29%20ELSE%20CHAR%2848%29%20END%29%29%2BCHAR%28113%29%2BCHAR%2898%29%2BCHAR%28118%29%2BCHAR%28122%29%2BCHAR%28113%29%29%29%20AND%20%28%27cipr%27%3D%27cipr HTTP/1.1"
"""#
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