$ 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
Note the ‘s’ ending leftmost non-overlapping occurrences.
re.subreplaces 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:
As you can see, there two (overlapping) occurrences of
BBBhere. Only the leftmost is replaced.