Does "Find-Replace whole word only" exist in python?
Does "Find-Replace whole word only" exist in 开发者_StackOverflowpython?
e.g. "old string oldstring boldstring bold" if i want to replace 'old' with 'new', new string should look like,
"new string oldstring boldstring bold"
can somebody help me?
>>> import re
>>> s = "old string oldstring boldstring bold"
>>> re.sub(r'\bold\b', 'new', s)
'new string oldstring boldstring bold'
This is done by using word boundaries. Needless to say, this regex is not Python-specific and is implemented in most regex engines.
you need the following regex:
\bold\b
精彩评论