I'd like to use a
variable inside a
regex, how can I do this in
TEXTO = sys.argv if re.search(r"\b(?=\w)TEXTO\b(?!\w)", subject, re.IGNORECASE): # Successful match else: # Match attempt failed
This question is related to
You have to build the regex as a string:
TEXTO = sys.argv my_regex = r"\b(?=\w)" + re.escape(TEXTO) + r"\b(?!\w)" if re.search(my_regex, subject, re.IGNORECASE): etc.
Note the use of
re.escape so that if your text has special characters, they won't be interpreted as such.