是否“仅查找-替换整个单词"?存在于python中? [英] Does "Find-Replace whole word only" exist in python?
本文介绍了是否“仅查找-替换整个单词"?存在于python中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
python 中是否存在仅查找替换整个单词"?
Does "Find-Replace whole word only" exist in python?
例如旧字符串 oldstring 粗体粗体"如果我想用 'new' 替换 'old',新字符串应该是这样的,
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"
"new string oldstring boldstring bold"
有人可以帮我吗?
推荐答案
>>> import re
>>> s = "old string oldstring boldstring bold"
>>> re.sub(r'\bold\b', 'new', s)
'new string oldstring boldstring bold'
这是通过使用词边界来完成的.不用说,这个正则表达式不是 Python 特定的,并且在大多数正则表达式引擎中都实现了.
This is done by using word boundaries. Needless to say, this regex is not Python-specific and is implemented in most regex engines.
这篇关于是否“仅查找-替换整个单词"?存在于python中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文