import Foundation
let pattern = #"(?:http(s)?://)?([a-z][-\w]+(?:\.\w+)+(?:\S+)?)"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
Here is a sentence with a url containing a query string: https://www.google.com/search?q=mickmackusa&oq=mickmackusa&aqs=chrome..69i57j69i60.271j0j7&sourceid=chrome&ie=UTF-8 all good.
http://google.com
http://google.com.au
https://google.com.au
www.google.com
google.com
I went to the local food store and bought some food.\n\nI was able to find everything
I went to the local food store and bought some food.
I was able to find everything
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"<a href="http$1://$2" target="_blank" title="$0">$0</a>"#
let result = regex.stringByReplacingMatches(in: testString, range: stringRange, withTemplate: substitutionString)
print(result)
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