繁体   English   中英

Python在字符串中找到所有模糊匹配序列

[英]Python find all fuzzy matching sequences in a string

我有一个大字符串,我想找到与此字符串匹配的所有输入序列。

因此,例如,我想在以下位置找到防守篮板的所有可能匹配项:

球员xy仅在比赛的第3季度才获得10个防守篮板 ,这是两支球队之间的防守战, 防守篮板率均超过80%,而且该球员在防守中的平均篮板数达到惊人的3.5

我想找到所有粗体字,然后将其提取出来。

我设法建立了执行提取的脚本,但它仅适用于完全匹配。

我当时在考虑使用difflib.SequenceMatcher但是我陷入了困境。

您可以在python中使用regex,并且应该使用goog模式提取它们。

例如:

import re

#Find [defence(s)][space][rebound(s)][space][any word]
re.findall('defensive[\w]* rebound[\w]* [\w]+', s)

#Find [rebound(s)][space][any word][space][any word][space][any word]
re.findall('rebound[\w]* [\w]+ [\w]+ [\w]+', s)

findall返回匹配列表

如果您所有的匹配项都使用粗体字形式,则可以使用以下方式将其提取:

re.findall('rebound[ \w]*defence', s)
re.findall('defensive[\w]* rebound[\w]*[ rate]*', s)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM