diff --git a/pendulum/formatting/formatter.py b/pendulum/formatting/formatter.py index ee04063f..f7ceb17f 100644 --- a/pendulum/formatting/formatter.py +++ b/pendulum/formatting/formatter.py @@ -376,8 +376,7 @@ def parse( """ escaped_fmt = re.escape(fmt) - tokens = self._FROM_FORMAT_RE.findall(escaped_fmt) - if not tokens: + if not self._FROM_FORMAT_RE.search(escaped_fmt): raise ValueError("The given time string does not match the given format") if not locale: @@ -405,7 +404,7 @@ def parse( lambda m: self._replace_tokens(m.group(0), loaded_locale), escaped_fmt ) - if not re.search("^" + pattern + "$", time): + if not re.fullmatch(pattern, time): raise ValueError(f"String does not match format {fmt}") def _get_parsed_values(m: Match[str]) -> Any: @@ -629,7 +628,6 @@ def _get_parsed_locale_value( match = "months.abbreviated" elif token == "Do": parsed["day"] = int(cast(Match[str], re.match(r"(\d+)", value)).group(1)) - return elif token == "dddd": unit = "day_of_week" diff --git a/pendulum/locales/locale.py b/pendulum/locales/locale.py index dba73bdf..35093075 100644 --- a/pendulum/locales/locale.py +++ b/pendulum/locales/locale.py @@ -48,7 +48,7 @@ def load(cls, locale: str | Locale) -> Locale: @classmethod def normalize_locale(cls, locale: str) -> str: - m = re.match("([a-z]{2})[-_]([a-z]{2})", locale, re.I) + m = re.fullmatch("([a-z]{2})[-_]([a-z]{2})", locale, re.I) if m: return f"{m.group(1).lower()}_{m.group(2).lower()}" else: diff --git a/pendulum/parsing/__init__.py b/pendulum/parsing/__init__.py index 908b670d..98f2b377 100644 --- a/pendulum/parsing/__init__.py +++ b/pendulum/parsing/__init__.py @@ -140,7 +140,7 @@ def _parse_common(text: str, **options: Any) -> datetime | date | time: :param text: The string to parse. """ - m = COMMON.match(text) + m = COMMON.fullmatch(text) has_date = False year = 0 month = 1 diff --git a/pendulum/parsing/iso8601.py b/pendulum/parsing/iso8601.py index cc4dd7aa..4f7f6f86 100644 --- a/pendulum/parsing/iso8601.py +++ b/pendulum/parsing/iso8601.py @@ -96,7 +96,7 @@ def parse_iso8601( if parsed is not None: return parsed - m = ISO8601_DT.match(text) + m = ISO8601_DT.fullmatch(text) if not m: raise ParserError("Invalid ISO 8601 string") @@ -263,7 +263,7 @@ def parse_iso8601( def _parse_iso8601_duration(text: str, **options: str) -> Duration | None: - m = ISO8601_DURATION.match(text) + m = ISO8601_DURATION.fullmatch(text) if not m: return None