python regex re.sub with strings <p>I am trying to use regex expressions to build a word filter. Currently, i have something in this form: </p> <pre><code>value = re.sub(r'(([q])[ ]*[w][ ]*[e][ ]*[r])', r'\2***', value, flags=re.IGNORECASE) </code></pre> <p>I would like to be able to do something like </p> <pre><code>value = regex_gen("qwer", value) </code></pre> <p>where my regex_gen function looks like: </p> <pre><code>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 </code></pre> <p>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</p>