import Foundation
let pattern = #"[^`\w@\\](t[xyq]|th(nx|nq|x)|than[xk](s)?(you|[syxzq]){0,1})\b"#
let regex = try! NSRegularExpression(pattern: pattern, options: .caseInsensitive)
let testString = ##"""
#- Should match
tx, ty, tq,
thnq, thx, thnx,
thanx, thanxyou, thanxs, thanxy, thanxx, thanxz, thanxq, thanxss, thanxsy, thanxsx, thanxsz, thanxsq
thank, thankyou, thanks, thanky, thankx, thankz, thankq, thankss, thanksy, thanksx, thanksz, thanksq
...
#- Shouldn't work
thanksk
thans
than
thanker
thanking
thxq
thnxs
txs
taxs
`thanks`
``` a real world testing of thanks regex ```
_thanks_
tank of pleasure
tax is high
taks away!
tanks are everywhere
@thanks buddy
# Shouldn't match
@thanks
@tanks
@tx
@ty
thankqx
"""##
let stringRange = NSRange(location: 0, length: testString.utf16.count)
let substitutionString = #"$1"#
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