If you do not need to use regEx, you can try using 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)
Artsiom Rudzenka
source share