开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜