简体   繁体   English

带有字符串的python regex re.sub

[英]python regex re.sub with strings

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: 我的regex_gen函数如下所示:

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. 但是我的regex_gen函数到目前为止无法正常工作,我只考虑了字符和字符大小写之间的空白。 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 . 当前,您的变量regexregex_results具有r'...' Change the code so that it doesn't add those characters on it. 更改代码,使其不会在其上添加这些字符。 For example replace: 例如替换:

regex = "r'(" into regex = "(" regex = "r'(" 变成 regex = "("
regex_result = "r'" into regex_result = "" regex_result = "r'" regex_result = ""
regex += ")'" into regex += ")" regex += ")'" regex += ")"
remove regex_result += "'" 删除 regex_result += "'"

And replace \\2 with \\\\2 . 并将\\2替换为\\\\2 For example: 例如:

regex_result += "\\2"

Now run your code again. 现在再次运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM