Literal parenthesis with python regex
parentheses, python, regex
Solution
You can use `re.escape` to handle this:
regexString = '(?<= = ")' + re.escape(original) + '(?=")'
Problem
I have a dictionary ( e.g. English - Croatian). It may contain sentences and phrases. I'm translating a file of form `"english text" = "english text"` into form `"english text" = "croatian text"` and using python regex module to do so. The regex I'm using looks like this (given variable original which is text in English that should be translated: ``` regexString = '(?<= = ")'+original+'(?=")' ``` That way I'am able to capture exactly the english text inside the quotes on the right-hand side and substitute it with Croatian. However, the problem appears if the original text contains parenthesis inside. In example: ``` original = 'This is a wonderland :)' ``` In that case an error "unbalanced parenthesis" is raised. If original would be hard-coded, I could solve the problem by putting ``` original = 'This is a wonderland :\\)' ``` However, there is a whole file full of *original * variables. Is there any solution to this problem other than changing original variable by preceeding all parenthesis in it with a backslash?