I am trying to use regex expressions to build a word filter. Currently, i have something in this form:
value = re.sub(r'(([q])[ ]*[w][ ]*[e][ ]*[r])', r'\2***', value, flags=re.IGNORECASE)
I would like to be able to do something like
value = regex_gen("qwer", value)
where my regex_gen function looks like:
def regex_gen(filter_word, string):
first = 0
regex = "r'("
regex_result = "r'"
for c in filter_word:
if first == 0:
regex += "([" + c + "])"
regex_result += "\2"
first += 1
else:
regex += "[ ]*[" + c + "]"
regex_result += "*"
regex += ")'"
regex_result += "'"
final = re.sub(regex, regex_result, string, flags=re.IGNORECASE)
return final
but my regex_gen function isn't working so far, i am only accounting for white spaces in between the characters and character case. if other approaches to a word filter are easier to implement than that would work too
Currently you have r'...'
with your variable regex
and regex_results
. Change the code so that it doesn't add those characters on it. For example replace:
regex = "r'("
into regex = "("
regex_result = "r'"
into regex_result = ""
regex += ")'"
into regex += ")"
remove regex_result += "'"
And replace \\2
with \\\\2
. For example:
regex_result += "\\2"
Now run your code again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.