how to write a regular expression to match series based on year range?

Viewed 34

I am trying to find matching year from a series id starting with 201x and 202x from file . I was thinking to solve this problem with a regex in order to extract match paterns

the following file contains an sample of my original file

NE112040
ENE112042
ENE112043
ENE112009
ENE112006
ENE112041
ENE112012
ENE112018
MEC112129
INF112094
2012030116
2012030395
2012030396
2012030364
2012030246

code to match numbers

def getNumbers(str): 
    array = re.findall(r'[0-9]+', str) 
    return array

once I found the matching patterns , I have to save into a new file with matching series

1 Answers

len(val) >= 4 and val[3].isdigit() and (val.startswith('201') or val.startswith('202'))

And if you prefer regex anyway:

re.match('(201|202)\d', val)

Related