Reputation: 2765
I have a list of bad word. Let's say it is:
BAD_WORDS = ['bw1', 'bw2',...]
Now I'm wondering what is the most efficient way to check a long string (aka a django request post) in a code like:
if re.search(comment.body) in BAD_WORDS:
dosomething;
Upvotes: 0
Views: 74
Reputation: 251166
You can use any
for this.
To match only the substring not exact word you can use the in
operator:
if any(word in comment.body for word in BAD_WORDS):
#do something
To match exact word use regex
:
import re
if any(re.search(r'\b{}\b'.format(re.escape(word)), comment.body)
for word in BAD_WORDS):
#do something
Upvotes: 1
Reputation: 24788
The best way is to use one expression for all the bad words:
import re
bad_words = ['bw1', 'bw2', ... ]
my_expression = '|'.join(re.escape(word) for word in bad_words)
if re.search(my_expression, comment.body):
do_something()
Upvotes: 2