How to substitute into a regular expression group in Python
- by peter
>>> 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?