i have a noob question.
I have a record in a table that looks like ‘\1abc’
I then use this string as a regex replacement in re.sub("([0-9])",thereplacement,"2")
I’m a little confused with the backslashes. The string i got back was "\\1abc"
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Are you using python interactivly?
In regular string you need to escape backslashes in your code, or use r”…” (Link to docs). If you are running python interactivly and don’t assign the results from your database to a variable, it’ll be printed out using it’s
__repr__()method.Also, your re.sub is a bit weird. 1) Maybe you meant [0-9] as the pattern? (Matching a single digit). The arguments are probably switche too, if
thereplacementis your input. This is the syntax:So my guess is you expect something like this:
Edit: Tried to better explain escaping/representation.