Home > OS >  Regex to match pattern of two digit number before a string
Regex to match pattern of two digit number before a string

Time:05-05

I have a text as so:

text = "There is 18 years of experience in marketing, with 5 years in sales."

I want to create a regex rule that will extract \d\d "years" so that it may extract any one and two digit number followed by the keyword 'years' or 'Years' or 'Yrs' from any corpus.

Expected output: ["18 year", "5 years"]

I tried

text = re.findall(r'\d\d  years', text)

which returned None

How to get this?

CodePudding user response:

Try this pattern: \d{1,2} (?:yrs|years?) with flag IGNORECASE in python.

See Regex Demo

Explanation

  • \d{1,2}: capture one or two-digit.
  • (?:: is a non-captured group.
  • yrs|years: this captures "yrs" or "years". (case-insensitive)
  • ?: this after "y" means "s" can be or not.
  • Related