# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\b(t)(?:anks *(?=[.?!]\n|to|for|in|ever)|[han]{3}([ks]{2}|x)+|hx|anx)\b"
test_str = ("Fix:\n\n"
"thx 28,227 results \n"
"thanx 12,147\n"
"Tanks 1,087 <-- may actually mean _tanks_.\n"
" tanks in advance\n"
" tanks everyone\n"
" tanks for the help\n"
" Tanks!\n"
"thansk 518\n"
"tanx 125\n"
"tahnks 97\n\n\n"
"Leave alone:\n\n"
"thank you\n"
"tank, tanks of acid,\n"
"task, tasks\n"
"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/)")
subst = "$1hanks"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.IGNORECASE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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 Python, please visit: https://docs.python.org/3/library/re.html