简体   繁体   中英

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

Does "Find-Replace whole word only" exist in python?

eg "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.

你需要以下正则表达式:

\bold\b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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