如何在字符串中匹配日期模式

时间:2022-07-07 18:45:59

I am trying to match any number of correct format dates in a given string. For a date to be in correct format, it has to appear in the form dd-mm-yyyy. Therefore dd and mm cannot have just one digit, they need 2 and they need to be in the correct range; this means that day has to be between 1 and 31 and month has to be between 1 and 12.

我正在尝试在给定的字符串中匹配任意数量的正确格式日期。要使日期格式正确,它必须以dd-mm-yyyy的形式出现。因此dd和mm不能只有一个数字,他们需要2,并且需要在正确的范围内;这意味着这一天必须在1到31之间,而月必须在1到12之间。

I have it working for one type of input, but it does not match another input. Here is my code:

我让它对一种输入有效,但它与另一种输入不匹配。这是我的代码:

#!/usr/bin/env python
from sys import stdin
from re import compile

myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[3-5]))' )
print myFormat.findall(stdin.readline())

Input 1:

输入1:

777-444---21-12-2013-12-2013-12-2013---444-777

Output:

输出:

[('21', '12', '2013'), ('13', '12', '2013'), ('13', '12', '2013')]

So far so good. But if I have the input:

目前为止一切都很顺利。但是如果我有输入

0012-10-2012-10-2012

it matches nothing. The correct output is supposed to be:

它匹配。正确的输出应该是:

[('12', '10', '2012'), ('12', '10', '2012')]

Please help me find the correct regex to do this

请帮我找到正确的regex

E: I only want to match only years 2012 to 2015.

艾凡:我只想把2012年和2015年比一比。

1 个解决方案

#1


3  

If you change your regex to:

如果您将regex更改为:

myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[2-5]))' )

it will work (just change last [3-5] to [2-5]). Currently it doesn't because you have:

它会起作用(只需将last[3-5]改为[2-5])。目前没有,因为你有:

201[3-5]

for the year part, so it refuses to match 2012.

在过去的一年里,它拒绝与2012年比赛。

For checking validity:

检查的有效性:

from sys import stdin
from re import compile
from datetime import datetime
myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[2-5]))' )
str1=("0012-10-2012-10-2012", "0031-02-2012");
for s in str1:
    for date in myFormat.findall(s):
        (d,m,y) = map(int, date)
        try:
           datetime(y,m,d)
           print date
        except: pass

#1


3  

If you change your regex to:

如果您将regex更改为:

myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[2-5]))' )

it will work (just change last [3-5] to [2-5]). Currently it doesn't because you have:

它会起作用(只需将last[3-5]改为[2-5])。目前没有,因为你有:

201[3-5]

for the year part, so it refuses to match 2012.

在过去的一年里,它拒绝与2012年比赛。

For checking validity:

检查的有效性:

from sys import stdin
from re import compile
from datetime import datetime
myFormat = compile(r'(?=([0-2]\d|3[0-1])-(0\d|1[0-2])-(201[2-5]))' )
str1=("0012-10-2012-10-2012", "0031-02-2012");
for s in str1:
    for date in myFormat.findall(s):
        (d,m,y) = map(int, date)
        try:
           datetime(y,m,d)
           print date
        except: pass