python中是否存在“Find Replace whole word only”?

2024-04-24 06:31:09 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
2条回答

您需要以下regex:

\bold\b
>>> import re
>>> s = "old string oldstring boldstring bold"
>>> re.sub(r'\bold\b', 'new', s)
'new string oldstring boldstring bold'

这是通过使用word boundaries完成的。不用说,这个regex不是特定于Python的,并且是在大多数regex引擎中实现的。

相关问题 更多 >