-2

I have a text string, similar to below example,

I have 5-6 year of experience with 2-3 years experience in Java

I have used this below regex syntax to match it,

import re

pattern = '\d{1}-\d{1} year'
[(m.start(0), m.end(0),'Experience') for m in re.finditer(pattern, string)]

# results
5-6 year
2-3 year (In this case it's missing out the 's'.)

How to modify this pattern to also match 'years and year' which every is longest?

user_12
  • 1,778
  • 7
  • 31
  • 72

1 Answers1

2

Add an optional "s": '\d{1,2}-\d{1,2}\s*years?'. I also changed '\d{1}' to '\d{1,2}' which means "one or two digits" (it's hard to imagine someone has more than 99 years of experience), and replaced one space with '\s*' - any number of spaces, including no spaces.

DYZ
  • 55,249
  • 10
  • 64
  • 93