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.'
Answer from Tim Pietzcker on Stack OverflowVideos
The two substitutions you'd want in your example overlap - the comma between your two instances of 't' will be matched by (.) in the first case, so ([^']) in the second case never gets a chance to match it. This slightly modified version might help:
line = re.sub(r"(?<!')'t'(?=.)", r"THIS_IS_TRUE", line)
This version uses lookahead and lookbehind syntax, described here.
How about
line = line.replace("'t'", "THIS_IS_TRUE").replace("'f'", "THIS_IS_FALSE")
without using re. This replaces all occurrences of 't' and 'f'. Just make sure that no car is named t.
You can replace it directly:
>>> import re
>>> s = 'sdfjoiweng%@$foo$fsoifjoi'
>>> print(re.sub('foo','bar',s))
sdfjoiweng%@$bar$fsoifjoi
It will also work for more occurrences of foo like below:
>>> s = 'sdfjoiweng%@$foo$fsoifoojoi'
>>> print(re.sub('foo','bar',s))
sdfjoiweng%@$bar$fsoibarjoi
If you want to replace only the 1st occurrence of foo and not all the foo occurrences in the string then alecxe's answer does exactly that.
re.sub(r'\bfoo\b', 'bar', s)
Here, the \b defines the word boundaries - positions between a word character (\w) and a non-word character - exactly what you have matching for foo inside the sdfjoiweng%@$foo$fsoifjoi string. Works for me:
In [1]: import re
In [2]: s = 'sdfjoiweng%@$foo$fsoifjoi'
In [3]: re.sub(r'\bfoo\b', 'bar', s)
Out[3]: 'sdfjoiweng%@$bar$fsoifjoi'