简体   繁体   English

如何编写正则表达式来匹配基于年份范围的系列?

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

I am trying to find matching year from a series id starting with 201x and 202x from file.我正在尝试从文件中以 201x 和 202x 开头的系列 ID 中查找匹配的年份。 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找到匹配模式后,我必须将其保存到具有匹配系列的新文件中

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM