I am trying to add a more efficient swear filter to a chat service we have and can’t seem to get my regular expression working on my live django server.
I am running the latest stable Django on Python 2.6.7.
Here is my code:
def replacement(match):
return "*" * len(match.group(0))
def censored_string(cFilter, dirty):
clean = str(dirty)
wordList = cFilter.mutations.split(',')
wordList.sort(key = len)
wordList.reverse()
for swear_word in wordList:
target_word = swear_word.strip()
result = re.sub("(?i)\\b(("+target_word+"){1,})(s{0,1})\\b",replacement, clean)
clean = result
return clean
for the record – this works using my local server setup which I can confirm is also using python 2.6.7 and the same django version, however I have not done much django or python since about 10 months ago and inherited this server setup recently – if there is something I am missing please let me know.
the output of the error is as follows:
{
"error_message": "multiple repeat",
"traceback": ... "result = re.sub(\"(?i)\\\\b(\"+target_word+\"){1,}(s{0,1})\\\\b\",censored_word(target_word), clean)\n\n File \"/usr/lib/python2.6/re.py\", line 151, in sub\n return _compile(pattern, 0).sub(repl, string, count)\n\n File \"/usr/lib/python2.6/re.py\", line 245, in _compile\n raise error, v # invalid expression\n\nerror: multiple repeat\n"
}
I have tried with and without greedy’s and so forth but am lost now – any input would be greatly appreciated
cheers,
Michael
I don’t think the problem is with the regex, but with your word list. It’s likely the list contains characters which are interpreted as regex special characters. This works for me: