Fix Python – How to use a variable inside a regular expression?


Asked By – Pedro Lobito

I’d like to use a variable inside a regex, how can I do this in Python?

TEXTO = sys.argv[1]

if"\b(?=\w)TEXTO\b(?!\w)", subject, re.IGNORECASE):
    # Successful match
    # Match attempt failed

Now we will see solution for issue: How to use a variable inside a regular expression?


From python 3.6 on you can also use Literal String Interpolation, “f-strings”. In your particular case the solution would be:

if"\b(?=\w){TEXTO}\b(?!\w)", subject, re.IGNORECASE): something


Since there have been some questions in the comment on how to deal with special characters I’d like to extend my answer:

raw strings (‘r’):

One of the main concepts you have to understand when dealing with special characters in regular expressions is to distinguish between string literals and the regular expression itself. It is very well explained here:

In short:

Let’s say instead of finding a word boundary \b after TEXTO you want to match the string \boundary. The you have to write:

TEXTO = "Var"
subject = r"Var\boundary"

if"\b(?=\w){TEXTO}\\boundary(?!\w)", subject, re.IGNORECASE):

This only works because we are using a raw-string (the regex is preceded by ‘r’), otherwise we must write “\\\\boundary” in the regex (four backslashes). Additionally, without ‘\r’, \b’ would not converted to a word boundary anymore but to a backspace!


Basically puts a backspace in front of any special character. Hence, if you expect a special character in TEXTO, you need to write:

if"\b(?=\w){re.escape(TEXTO)}\b(?!\w)", subject, re.IGNORECASE):

NOTE: For any version >= python 3.7: !, ", %, ', ,, /, :, ;, <, =, >, @, and ` are not escaped. Only special characters with meaning in a regex are still escaped. _ is not escaped since Python 3.3.(s. here)

Curly braces:

If you want to use quantifiers within the regular expression using f-strings, you have to use double curly braces. Let’s say you want to match TEXTO followed by exactly 2 digits:

if"\b(?=\w){re.escape(TEXTO)}\d{{2}}\b(?!\w)", subject, re.IGNORECASE):

This question is answered By – airborne

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0