简体   繁体   English

正则表达式模式搜索日期格式 MMDDYYYY(python)

[英]regex pattern search for date format MMDDYYYY(python)

I have a list of dates, I want to search for MMDDYYYY or DDMMYYYY or YYYYMMDD using regex.我有一个日期列表,我想使用正则表达式搜索 MMDDYYYY 或 DDMMYYYY 或 YYYYMMDD。 I have been using dateutil and regex pattern to find them but I later realized that dateutil doesn't match for this formats.我一直在使用 dateutil 和 regex 模式来查找它们,但后来我意识到 dateutil 与这种格式不匹配。 SO I used regex but the regex pattern matches all kind of values(eg: 55122020 )maybe it is counting it has integers.所以我使用了正则表达式,但正则表达式模式匹配所有类型的值(例如: 55122020 )也许它正在计算它有整数。 Is there any pattern which can able to match this kind of Date formats?有没有可以匹配这种日期格式的模式?

lst = ['2020/12/22','20200322', '34252020']
D = r'^(?:(?:19|20)\d{2}([-/]?)\d{1,2}\1\d{1,2}|\d{1,2}([-/]?)\d{1,2}\2(?:19|20)\d{2})$'
for i in lst:
    if re.search(D, str(i)) != None:
        print(i)
    else:
        print('not matched')

Output:
2020/12/22
20200322
34252020

But in actual real world the last value is invalid so the output should be 'not matched'.但在实际现实世界中,最后一个值无效,因此 output 应该“不匹配”。 Is there any pattern which matches this scenario?有没有与这种情况相匹配的模式?

Actual output:
2020/12/22
20200322
not matched

You can use the built-in dateutil.parser.parse()您可以使用内置的dateutil.parser.parse()

from dateutil.parser import parse

dates = ['2020/12/22','20200322', '34252020']
for d in dates:
    try:
        d = parse(d)
        print(d)
    except:
        print(d, "isn't a date")

2020-12-22 00:00:00
2020-03-22 00:00:00
34252020 isn't a date

Demo演示


Note:笔记:

To parse multiple dates in a str , you may also want to use datefinder :要解析str中的多个日期,您可能还需要使用datefinder

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

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