首页 > 解决方案 > 在python中查找字符串中单词完全匹配的所有位置

问题描述

我正在尝试在文本中查找给定单词(类型字符串)的所有完整实例的所有起始索引。

例子:

词=“黑客”

text = " 安全黑客是探索破坏防御和利用计算机系统或网络中的弱点的方法的人。黑客可能受到多种原因的激励,例如利润、抗议、信息收集、[2] 挑战、娱乐、 [3] 或评估系统弱点以协助制定针对潜在黑客的防御措施。围绕黑客发展的亚文化通常被称为“地下计算机”

输出将是:[11]

我使用了 finditer 方法,但它返回错误的索引。

标签: pythonre

解决方案


要获得整个单词的所有结果,您可以\b在正则表达式模式中使用词缀。

word = "hacker"

text = """A security hacker is someone who explores methods for breaching defenses and exploiting weaknesses in a computer system or network. Hackers may be motivated by a multitude of reasons, such as profit, protest, information gathering,[2] challenge, recreation,[3] or to evaluate system weaknesses to assist in formulating defenses against potential hackers. The subculture that has evolved around hackers is often referred to as the "computer underground" """

pattern = re.compile(r'\b' + word + r'\b')
for m in re.finditer(pattern, text):
    idx = m.start(0)
    print(idx, text[idx:idx+len(word)])

我们得到正确的输出

11 黑客


推荐阅读