Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

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'
like image 517
Moondra Avatar asked Aug 14 '17 20:08

Moondra


1 Answers

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

like image 98
anubhava Avatar answered Oct 03 '22 08:10

anubhava