How to replace into a regex group in Python
>>> s = 'foo: "apples", bar: "oranges"' >>> pattern = 'foo: "(.*)"' I want to be able to substitute in the group:
>>> re.sub(pattern, 'pears', s, group=1) 'foo: "pears", bar: "oranges"' Is there a good way to do this?
For me, something like works:
rx = re.compile(r'(foo: ")(.*?)(".*)') s_new = rx.sub(r'\g<1>pears\g<3>', s) print(s_new) Pay attention to ? in re, so it ends first, " also pay attention to " in groups 1 and 3, because they must be output.
Instead of \g<1> (or \g<number> ) you can use only \1 , but do not forget to use raw strings and that the form g<1> preferable because \1 can be ambiguous (look for examples in Python doc ).
re.sub(r'(?<=foo: ")[^"]+(?=")', 'pears', s) A regular expression matches a sequence of characters that
- Fulfills the line
foo: ", - does not contain double quotes and Per
- follows
"
(?<=) and (?=) are lookbehind and lookahead
This regular expression will not be executed if the foo value contains escaped quotas. Use the following command to catch them:
re.sub(r'(?<=foo: ")(\\"|[^"])+(?=")', 'pears', s) Code example
>>> s = 'foo: "apples \\\"and\\\" more apples", bar: "oranges"' >>> print s foo: "apples \"and\" more apples", bar: "oranges" >>> print re.sub(r'(?<=foo: ")(\\"|[^"])+(?=")', 'pears', s) foo: "pears", bar: "oranges"