Jan-16-2021, 11:04 AM
(Jan-15-2021, 08:20 AM)Serafim Wrote: When I wrote my post yesterday I was pretty tired. Maybe an explanation is in place.Thank you for taking the time to reply!! Code doesn't come easy to me so its great to learn from things like this, I really appreciate it thank you!
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, ***** *****, ***** >>>
:)