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'
You can use a lookbehind and lookahead based regex and then a lambda
function to iterate through replacements words:
>>> words = ['Swimming', 'Eating', 'Jogging']
>>> pattern = re.compile(r'(?<=I love )\w+(?=\.)')
>>> print pattern.sub(lambda m: words.pop(0), string)
'I love Swimming. I love Eating. I love Jogging.'
Code Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With