我有一个短语列表(由用户输入)我想在文本文件中找到它们,例如:
titles = ['Blue Team', 'Final Match', 'Best Player',]
text = 'In today Final match, The Best player is Joe from the Blue Team and the second best player is Jack from the Red team.'
1./我可以像这样找到这些短语的所有出现
titre = re.compile(r'(?P<title>%s)' % '|'.join(titles), re.M)
list = [ t for t in titre.split(text) if titre.search(t) ]
(为简单起见,我假设一个完美的间距。)
2./我还可以找到这些短语的变体,例如“蓝队”、“决赛”、“最佳球员”……如果它们出现在文本中,则使用 re.I。
但我想限制在文本中仅查找首字母大写的输入短语的变体,例如“Blue team”,无论它们是如何作为输入输入的,例如“bluE tEAm”。
是否可以写一些东西来“阻止”部分短语的 re.I 标志?在伪代码中,我想生成类似“[B]lue Team|[F]inal Match”的东西。
注意:我的主要目标不是,例如,计算文本中输入短语的频率,而是提取和分析它们之间或周围的文本片段。