Replace all occurrences that match regular expression

re.sub() (docs for Python 2 and Python 3) does replace all matches it finds, but your use of .* may have caused the regex to match too much (even other occurences of .00. etc.). Simply do:

In [2]: re.sub(r"\.(00|11)\.", r"X\1X", ".00..0..11.")
Out[2]: 'X00X.0.X11X'

Note that patterns cannot overlap:

In [3]: re.sub(r"\.(00|11)\.", r"X\1X", ".00.11.")
Out[3]: 'X00X11.'

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)