Home > Mobile >  How to match pattern in regex using python and optimize the code
How to match pattern in regex using python and optimize the code

Time:01-10

I'm having a pattern where it should be matched any where in the input text given

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)

There are several text patterns repeated, for example "weather" is redundant because "Todays weather" already matches "weather" . Any solution to optimize the code would really helps a lot.

CodePudding user response:

You could make the pattern case insensitive using re.I, make some of the parts optional so that you can shorted the alternatives and put all the alternatives in a non capture group adding word boundaries to the left and right (or keep the spaces if you want)

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

See a regex 101 demo.

If you want to print all matches, you can use 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))

Output

['todays weather']
  • Related