【发布时间】:2016-11-15 06:45:13
【问题描述】:
我有一个字符串“the then there”,我想搜索准确/完整的单词,例如在这种情况下,“the”只出现一次。但是使用 index() 或 find() 方法认为出现了 3 次,因为它也与 "then" 和 "there" 部分匹配。我喜欢使用这两种方法中的任何一种,有什么方法可以调整它们以使其工作吗?
>>> s = "the then there"
>>> s.index("the")
0
>>> s.index("the",1)
4
>>> s.index("the",5)
9
>>> s.find("the")
0
>>> s.find("the",1)
4
>>> s.find("the",5)
9
【问题讨论】:
-
使用正则表达式
\bthe\b