I'm looking to match years between 1980 and 2050 in sentences, using a regex.
So far I use:
def within_years(d):
return re.search('20[0-5][0-9]', d) or re.search('19[89][0-9]', d)
The problem now is that I also match "22015".
So I thought to prepend [^0-9]
, but then it cannot match the year if it is in the start of a sentence.
Next thing was to prepend [ /-]*
, but then it is still only optional.
Some examples:
should_match = ['2015 is a great year', 'best year: 2015']
should_not_match = ['22015 bogus', 'a2015 is not a year']