import Foundation
let pattern = #"^Content-Type: \K.+"#
let regex = try! NSRegularExpression(pattern: pattern, options: .anchorsMatchLines)
let testString = #"""
HTTP/1.1 200 OK
Date: Mon, 17 Jun 2019 19:32:37 GMT
Server: Apache/2.4.38 (Win32) OpenSSL/1.1.1a PHP/7.2.15
X-Powered-By: PHP/7.2.15
Set-Cookie: Avisistema=ihk9t1ms6r8i0u1j5t6jpjrqei; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: Avisistema=ihk9t1ms6r8i0u1j5t6jpjrqei; expires=Tue, 18-Jun-2019 03:32:37 GMT; Max-Age=28799; path=/; HttpOnly
Set-Cookie: SageFirmeware=Avisistema.A.0.2015.01.10; expires=Tue, 18-Jun-2019 03:32:37 GMT; Max-Age=28799; path=/; HttpOnly
Content-Length: 232
Content-Type: application/json
Some-Garbage: test string
"""#
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