Vaibhav Bajpai
Vaibhav Bajpai

Reputation: 16784

re.sub(...) replacing leftmost occurrences?

$ pydoc re.sub :

sub(pattern, repl, string, count=0, flags=0)
    Return the string obtained by replacing the leftmost
    non-overlapping occurrences of the pattern in string by the
    replacement repl.

>>> re.sub('ROAD', 'RD.', 'BRRROADBBRROAD ROAD ROAD MY ROAD')
'BRRRD.BBRRD. RD. RD. MY RD.'

I don't quite understand the meaning of leftmost in the python documentation. As far as I can see, it seems re.sub(...) is replacing all occurrences of pattern with repl

Upvotes: 8

Views: 3832

Answers (2)

Ray Toal
Ray Toal

Reputation: 88428

You can see what leftmost means in this example

>>> import re
>>> re.sub('haha', 'Z', 'hahaha')
'Zha'

Note we did not see 'haZ' which would have been rightmost substitution.

Upvotes: 6

Eli Bendersky
Eli Bendersky

Reputation: 273646

Note the 's' ending leftmost non-overlapping occurrences.

re.sub replaces all occurrences. You can use the optional count argument to limit the amount of replacements it does.

"Leftmost non-overlapping" means that if several occurrences are overlapping and can be potentially replaced, only the leftmost will:

>>> str = 'AABBBBAA'
>>> re.sub('BBB', 'CCC', str)
'AACCCBAA'

As you can see, there two (overlapping) occurrences of BBB here. Only the leftmost is replaced.

Upvotes: 12

Related Questions