简体   繁体   English

python中是否存在“查找 - 替换整个单词”?

[英]Does “Find-Replace whole word only” exist in python?

Does "Find-Replace whole word only" exist in python? python中是否存在“查找 - 替换整个单词”?

eg "old string oldstring boldstring bold" if i want to replace 'old' with 'new', new string should look like, 例如“old string oldstring boldstring bold”如果我想用'new'替换'old',新字符串应该是这样的,

"new string oldstring boldstring bold" “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. 毋庸置疑,这个正则表达式不是特定于Python的,并且在大多数正则表达式引擎中实现。

你需要以下正则表达式:

\bold\b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM