import Foundation
let pattern = #"\b(t)(?:anks *(?=[.?!]\n|to|for|in|ever)|[han]{3}([ks]{2}|x)+|hx|anx)\b"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = #"""
Fix:
thx 28,227 results
thanx 12,147
Tanks 1,087 <-- may actually mean _tanks_.
tanks in advance
tanks everyone
tanks for the help
Tanks!
thansk 518
tanx 125
tahnks 97
Leave alone:
thank you
tank, tanks of acid,
task, tasks
Suppose there is a production line with 8 tanks: each filled with a different substance for parts to be dipped in. The parts will be dropped into tanks by a crane along side the tanks. (http://stackoverflow.com/questions/25062870/)
"""#
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1hanks"#
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