简体   繁体   中英

How to match a date pattern in strings

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.

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:

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

EDIT

I only want to match only years 2012 to 2015.

If you change your regex to:

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:

201[3-5]

for the year part, so it refuses to match 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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