Replacing only the captured group using re.sub and multiple replacements

Viewed 2856

Below is just a simple example I've created.

string = 'I love sleeping. I love singing. I love dancing.'
pattern =re.compile(r'I love (\w+)\.')

I want to just replace the (\w+) portion with re.sub.
This question is in two parts:

I want to replace (\w+), without having to resort to groups to capture the rest of the text.

So I don't want to do something like:

pattern =re.compile(r'(I) (love) (\w+)\.')
re.sub(pattern, r'/1 /2 swimming', string)

because this may be unreliable when working with huge amounts of text and optional groups.

Second part:

Since I will have three matches, is it possible to feed in a list with re.sub that will iterate through the list for each match and make the sub accordingly. In another words, I want each item of the list ['Swimming, Eating, Jogging'] to sync with the matches, (like the method zip) and make the substitution.

So the output should be something like this (even a single total output is fine:

'I love Swimming'
'I love Eating'
'I love Jogging'
1 Answers
Related