如何使用python在regex中匹配模式并优化代码

bejyjqdl  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(183)

我有一个模式,它应该匹配给定输入文本中的任何位置

text_pattrn = """ Todays weather | Todays weather is | weather | Todays weather condition | weather condition """

input_text = """ Every one is eagerly waiting for the final match to be happened in this ground and all eyes on sky as todays weather condition is cloudy and rain may affect the game play"""

match = re.search(text_pattrn,input_text)

有几个文本模式重复,例如“天气”是多余的,因为“今天的天气”已经匹配“天气”。任何优化代码的解决方案都会有很大帮助。

thtygnil

thtygnil1#

您可以使用re.I使模式不区分大小写,使一些部分可选,以便您可以缩短替代项,并将所有替代项放在一个非捕获组中,在左右两侧添加单词边界(如果需要,也可以保留空格)

\b(?:Todays weather(?: is)?|weather|(?:Todays )?weather condition)\b

参见regex 101 demo
如果要打印所有匹配项,可以使用 * re. findall *

import re

text_pattrn = r"\b(?:Todays weather(?: is)?|weather|(?:Todays )?weather condition)\b"
input_text = """ Every one is eagerly waiting for the final match to be happened in this ground and all eyes on sky as todays weather condition is cloudy and rain may affect the game play"""
print(re.findall(text_pattrn, input_text, re.I))

产出

['todays weather']

相关问题