Legend
Legend

Reputation: 117000

Replacing each match with a different word

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?

Upvotes: 8

Views: 502

Answers (3)

John La Rooy
John La Rooy

Reputation: 304463

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)

Upvotes: 5

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29121

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)

Upvotes: 6

Qtax
Qtax

Reputation: 33928

You can use a callback function as the replace parameter, see how at:

http://docs.python.org/library/re.html#re.sub

Then use some counter and replace depending on the counter value.

Upvotes: 2

Related Questions