I need to replace a particular word abc
with abc_test
, but with \b
, .
is considered as word boundary so I get unexpected result. Can someone help me figure out how can I skip replacement for .
with /b
?
This is my script:
import re
f = open("4.txt", "r").read()
op = re.sub(r'\babc\b', 'abc_test', f)
print(op)
This is my input file 4.txt:
abc
abc xyz
abc
abc.asd
Output:
abc_test
abc_test xyz
abc_ztx
abc_test.asd # This gets replaced which is not as expected
Expected output:
abc_test
abc_test xyz
abc_ztx
abc.asd # This shouldn't be replaced
How can I restrict the regex to skip abc.asd
here?