From 2e68c720d50806b91b3d025eb5cbafd3a0957b44 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Tue, 20 Jan 2015 17:52:55 +0100 Subject: Don't match garbage characters at the end of date strings Addresses #15. --- src/isodate/isodates.py | 54 +++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/isodate/isodates.py b/src/isodate/isodates.py index 6660245..a58a2c0 100644 --- a/src/isodate/isodates.py +++ b/src/isodate/isodates.py @@ -67,50 +67,52 @@ def build_date_regexps(yeardigits=4, expanded=False): sign = 1 else: sign = 0 + def add_re(regex_text): + cache_entry.append(re.compile('\A' + regex_text + '\Z')) # 1. complete dates: # YYYY-MM-DD or +- YYYYYY-MM-DD... extended date format - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"-(?P[0-9]{2})-(?P[0-9]{2})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"-(?P[0-9]{2})-(?P[0-9]{2})" + % (sign, yeardigits)) # YYYYMMDD or +- YYYYYYMMDD... basic date format - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"(?P[0-9]{2})(?P[0-9]{2})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"(?P[0-9]{2})(?P[0-9]{2})" + % (sign, yeardigits)) # 2. complete week dates: # YYYY-Www-D or +-YYYYYY-Www-D ... extended week date - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"-W(?P[0-9]{2})-(?P[0-9]{1})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"-W(?P[0-9]{2})-(?P[0-9]{1})" + % (sign, yeardigits)) # YYYYWwwD or +-YYYYYYWwwD ... basic week date - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})W" - r"(?P[0-9]{2})(?P[0-9]{1})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})W" + r"(?P[0-9]{2})(?P[0-9]{1})" + % (sign, yeardigits)) # 3. ordinal dates: # YYYY-DDD or +-YYYYYY-DDD ... extended format # YYYYDDD or +-YYYYYYDDD ... basic format - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"-?(?P[0-9]{3})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"-?(?P[0-9]{3})" + % (sign, yeardigits)) # 4. week dates: # YYYY-Www or +-YYYYYY-Www ... extended reduced accuracy week date # YYYYWww or +-YYYYYYWww ... basic reduced accuracy week date - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"-?W(?P[0-9]{2})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"-?W(?P[0-9]{2})" + % (sign, yeardigits)) # 5. month dates: # YYY-MM or +-YYYYYY-MM ... reduced accuracy specific month - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - r"-(?P[0-9]{2})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + r"-(?P[0-9]{2})" + % (sign, yeardigits)) # 6. year dates: # YYYY or +-YYYYYY ... reduced accuracy specific year - cache_entry.append(re.compile(r"(?P[+-]){%d}(?P[0-9]{%d})" - % (sign, yeardigits))) + add_re(r"(?P[+-]){%d}(?P[0-9]{%d})" + % (sign, yeardigits)) # 7. century dates: # YY or +-YYYY ... reduced accuracy specific century - cache_entry.append(re.compile(r"(?P[+-]){%d}" - r"(?P[0-9]{%d})" - % (sign, yeardigits - 2))) + add_re(r"(?P[+-]){%d}" + r"(?P[0-9]{%d})" + % (sign, yeardigits - 2)) DATE_REGEX_CACHE[(yeardigits, expanded)] = cache_entry return DATE_REGEX_CACHE[(yeardigits, expanded)] -- cgit v1.2.1