Jan-15-2021, 08:20 AM
When I wrote my post yesterday I was pretty tired. Maybe an explanation is in place.
The "IGNORECASE" flag is pretty obvious, it secures matching regardless of case, the "escape" function escapes all special characters in the pattern "i", if any. I would maybe use better names, but that is mostly left to personal taste.
You can use other flags and combine them with "|" (bitwise OR). It is unicode safe unless you specify a langauage or ASCII flag. Best to consult https://docs.python.org/.
I tested it this morning:
The "IGNORECASE" flag is pretty obvious, it secures matching regardless of case, the "escape" function escapes all special characters in the pattern "i", if any. I would maybe use better names, but that is mostly left to personal taste.
You can use other flags and combine them with "|" (bitwise OR). It is unicode safe unless you specify a langauage or ASCII flag. Best to consult https://docs.python.org/.
I tested it this morning:
>>> import re >>> def censor2(text, banned): ... for i in banned: ... replace = re.compile(re.escape(i), re.IGNORECASE) ... substitute = '*'*len(i) ... text = replace.sub(substitute, text) ... return text >>> print(censor2('Hello World, hElLo IDIOT, HeLlO', ['hello', 'idiot'])) ***** World, ***** *****, ***** >>>