python - 在python中查找字符串中单词完全匹配的所有位置
问题描述
我正在尝试在文本中查找给定单词(类型字符串)的所有完整实例的所有起始索引。
例子:
词=“黑客”
text = " 安全黑客是探索破坏防御和利用计算机系统或网络中的弱点的方法的人。黑客可能受到多种原因的激励,例如利润、抗议、信息收集、[2] 挑战、娱乐、 [3] 或评估系统弱点以协助制定针对潜在黑客的防御措施。围绕黑客发展的亚文化通常被称为“地下计算机”
输出将是:[11]
我使用了 finditer 方法,但它返回错误的索引。
解决方案
要获得整个单词的所有结果,您可以\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 黑客
推荐阅读
- c++ - 对 C++17 Filesystem::last_write_time 的向量进行排序的问题
- javascript - 角度上传 - 无法查看旧数据
- java - Apache Log4j2 StrSubstitutor“默认变量”不起作用
- python - 安装的模块返回 ModuleNotFoundError
- rest - 这里 API 返回 Unrecognized Kid Null 错误
- python - 在字典中检查字典中python中的特定值
- flutter - 如何在颤动中输入数字的某些位置使用连字符制作文本字段
- r - 跨 R 项目传输对象的最佳实践是什么?
- javascript - Exceljs追加行
- c# - 更新正则表达式以仅允许单个空格