简体   繁体   中英

python date regex expression

I am trying to only add dates to my list.

pattern_six = re.compile(r'\d{1,2}-\d{1,2}-\d{4}')

Unfortunately, this regex expression picks up #2-1 as well, which is not a date.
Question 1: I thought the regex expression would only follow XXX pattern, not XX as well. Why is my assumption wrong?
Question 2: How can I fix my expression to correctly pick up only dates? I tried with this expression but failed.

pattern_six = re.compile(r'[^#](\d{1,2}-\d{1,2}-\d{4})')

Thank you for your help in advance :)

I think this is what you need

((0[1-9]|[12]\\d|3[01])-(0[1-9]|1[0-2])-[12]\\d{3})

https://regex101.com/r/qC9cH4/56

This is not a problem you should try to solve with regex. Why? See Jeppe Spanggaard answer. It seems correct, but what about:

>>> re.match("((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-[12]\d{3})", "31-02-2019")
<_sre.SRE_Match object; span=(0, 10), match='31-02-2019'>

But there is no 31th of february in 2019 (or in any other year)! Regex deal with patterns, but dates are not just patterns, dates are not digits and dashes. They are far more tricky (different number of days for different months, leap years, ...). Unless you have a very good reason, you should always use a library to handle dates (whatever language you use):

>>> from datetime import datetime as dt
>>> dt.strptime("31-02-2019", "%d-%m-%Y")
...
ValueError: day is out of range for month
>>> dt.strptime("28-02-2019", "%d-%m-%Y")
datetime.datetime(2019, 2, 28, 0, 0)

You can wrap strptime if you want to test if a string is a date in the format you want:

>>> def to_date(s):
...     try:
...         return dt.strptime(s, "%d-%m-%Y")
...     except ValueError:
...         return None
... 
>>> to_date("31-02-2019") is None
True
>>> to_date("28-02-2019")
datetime.datetime(2019, 2, 28, 0, 0)

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