>>> s = 'foo: "apples", bar: "oranges"'
>>> pattern = 'foo: "(.*)"'
I want to be able to substitute into the group like this:
>>> re.sub(pattern, 'pears', s, group=1)
'foo: "pears", bar: "oranges"'
Is there a nice way to do this?
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.
For me works something like:
Notice
?in re, so it ends with first", also notice"in groups 1 and 3 because they must be in output.Instead of
\g<1>(or\g<number>) you can use just\1, but remember to use “raw” strings and thatg<1>form is preffered because\1could be ambiguous (look for examples in Python doc) .