在 python 中查找字符串中单词完全匹配的所有位置
Finding all positions of full matches of a word in a string in python
我正在尝试查找文本中给定单词(字符串类型)的所有 完整实例 的所有起始索引。
示例:
word = "黑客"
text = " 安全黑客是探索突破防御和利用计算机系统或网络弱点的方法的人。黑客的动机可能有多种,例如利润、抗议、信息收集,[2 ] 挑战,娱乐,[3] 或评估系统弱点以协助制定针对潜在黑客的防御措施。围绕黑客发展的亚文化通常被称为“地下计算机”“
输出将是:[11]
我使用了 finditer 方法,但它 return 错误的索引。
要获得整个单词的所有结果,您可以在正则表达式模式中使用 \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 hacker
我正在尝试查找文本中给定单词(字符串类型)的所有 完整实例 的所有起始索引。
示例:
word = "黑客"
text = " 安全黑客是探索突破防御和利用计算机系统或网络弱点的方法的人。黑客的动机可能有多种,例如利润、抗议、信息收集,[2 ] 挑战,娱乐,[3] 或评估系统弱点以协助制定针对潜在黑客的防御措施。围绕黑客发展的亚文化通常被称为“地下计算机”“
输出将是:[11]
我使用了 finditer 方法,但它 return 错误的索引。
要获得整个单词的所有结果,您可以在正则表达式模式中使用 \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 hacker