我今天发现Python 3中的函数string.replace(str1, str2)
以及Python 2的行为并不像我本能地认为的那样:
$ python3
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> str = ' not not not not Cannot not not not '.replace(' not ', ' NOT ')
>>> str
' NOT not NOT not Cannot NOT not NOT '
我理解为什么会发生这种情况:replace
函数一旦找到匹配项,就会在上一次找到匹配后的第一个字符上继续,在我的情况下恰好是n
。因此,第二个(和第四个......)not
永远不会被识别,因为缺少领先的空间。
替换字符串的标准方法是什么,以避免上面的反直觉行为(以便所有␣not␣
都大写)?
我知道我可以将我的字符串拆分为takens,将not
更改为NOT
并重新组合,但这不是我要找的。我怀疑Python中有适当的替换方式。
答案 0 :(得分:10)
import re
s = re.sub(r"\bnot\b", "NOT", s)
使用正则表达式匹配单词边界,而不是尝试匹配单词之间的空格。