Python在一个字符串中查找所有的模糊匹配序列

2024-09-29 17:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很大的字符串,我想找到这个字符串中匹配的所有输入序列。在

例如,我想找到所有可能匹配的防守篮板

Player xy had 10 defensive rebounds only in the 3rd quarter of a match that was a defensive battle between 2 teams that have a defensive rebound rate of over 80% and moreover the average number of rebounds in the defence by player was a staggering 3.5

我想找到所有的粗体字,然后把它们提取出来。在

我设法构建了一个脚本来执行提取,但它只适用于精确匹配。在

我本来想用difflib.SequenceMatcher但我被卡住了。在


Tags: ofthe字符串inonlythat序列player
1条回答
网友
1楼 · 发布于 2024-09-29 17:19:23

您可以在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返回匹配列表

如果所有匹配项都是相同的粗体字形式,则可以使用以下命令提取:

^{pr2}$

相关问题 更多 >

    热门问题