combining raw strings with non raw string

33 views Asked by At

I want to combine a raw string with a non raw one which should be a user input (direct input or config file extracted variable), in order to combine them into a regular expression;

import re
input = "user_input"
regex = re.compile(r"^line:{}.\s(group_to_be_captured)".format(input)

The problem is that user_input contains parenthesis then

regex.pattern

is equal to

 '^line:userinputpart1(userinputpart2).\\s(group_to_be_captured)'

so \s has its slash escaped normally, but the parentheses in userinput are not escaped which is thus inconvenient since are recognized as groups to retrieve and not as characters.
I know that I indeed can replace them by escaped parentheses using re.sub or str.replace but it made me wonder if there is a simple mean to combine raw strings with non raw ones or if there is some format parameter I am missing ?

0

There are 0 answers