Was wondering what is the best way to match "clear 18+ from your history" from "clear 18+ from your history? blah blah blah" is? Using Python.
I've tried this,
keyword = "clear 18+ from your history"
prepped_string = "clear 18+ from your history? blah blah blah"
is_flagged = False
if re.search(r'\b' + keyword + r'\b', prepped_string):
is_flagged = True
The above code only works with no special character. If there is a special character like the plus sign, it won't work. Thanks in advance.
Here's the full code:
def _get_user_blacklist_weights(self, prepped_string, user):
"""
Returns a list of (word, weight, reason) for every user word that is found.
"""
out = []
if user.blacklist:
matches = user.blacklist.search(prepped_string)
for match in matches:
is_flagged = False
try:
if re.search(r'\b' + keyword + r'\b', prepped_string):
is_flagged = True
except Exception as e:
# The condition below fixes both python 3.4 and 3.6 error message on repeating characters.
if (str(e)).startswith(C.REPEAT_ERROR_MESSAGES):
is_flagged = True
else: # pragma: no cover
error_logging(e)
if is_flagged:
out.append((match, C.USER_BLACKLIST_MATCH_WEIGHT,
'%s or one of his/her accountability partners asked that "%s" be flagged.' % (user.person.first_name.title(), match)))
return out