0

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 ?

Ando Jurai
  • 828
  • 1
  • 11
  • 25
  • 2
    `.format(re.escape(input))` – Wiktor Stribiżew Sep 14 '17 at 14:02
  • 2
    You might already know this, but just to clarify: there is no raw string type which is distinct from the ordinary string type. Raw strings are only an alternative way of writing string literals. It's the same way that `5` and `0b101` both evaluate to identical integers, and there is no "boolean integer" type. – Kevin Sep 14 '17 at 14:03
  • @Kevin I think you meant "binary integer"? – Aran-Fey Sep 14 '17 at 14:04
  • Oops. Yes, I meant "binary integer". – Kevin Sep 14 '17 at 14:05
  • Well, I know there is no specific type still there is a specific way to assign them to variable, hence to "signal" they are to be treated in a different manner before being stored or processed. I guessed that there would be the complementary manner to specify behavior towards non raw strings (those you might want to be escaped). But actually, they made another call. Thanks for the lightning fast answer, I looked other answers but my way of formulating the problem was too much different, sorry for the duplicate. Thanks a lot! – Ando Jurai Sep 14 '17 at 14:10

0 Answers0