Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing each match with a different word

Tags:

python

regex

I have a regular expression like this:

findthe = re.compile(r" the ")
replacement = ["firstthe", "secondthe"]
sentence = "This is the first sentence in the whole universe!"

What I am trying to do is to replace each occurrence with an associated replacement word from a list so that the end sentence would look like this:

>>> print sentence
This is firstthe first sentence in secondthe whole universe

I tried using re.sub inside a for loop enumerating over replacement but it looks like re.sub returns all occurrences. Can someone tell me how to do this efficiently?

like image 804
Legend Avatar asked Jul 14 '11 04:07

Legend


2 Answers

If it is not required to use regEx than you can try to use the following code:

replacement = ["firstthe", "secondthe"]
sentence = "This is the first sentence in the whole universe!"

words = sentence.split()

counter = 0
for i,word in enumerate(words):
    if word == 'the':
        words[i] = replacement[counter]
        counter += 1

sentence = ' '.join(words)

Or something like this will work too:

import re
findthe = re.compile(r"\b(the)\b")
print re.sub(findthe, replacement[1],re.sub(findthe, replacement[0],sentence, 1), 1)

And at least:

re.sub(findthe, lambda matchObj: replacement.pop(0),sentence)
like image 92
Artsiom Rudzenka Avatar answered Oct 07 '22 19:10

Artsiom Rudzenka


Artsiom's last answer is destructive of replacement variable. Here's a way to do it without emptying replacement

re.sub(findthe, lambda m, r=iter(replacement): next(r), sentence)
like image 31
John La Rooy Avatar answered Oct 07 '22 21:10

John La Rooy