diff --git a/doc/source/whatsnew/v2.3.3.rst b/doc/source/whatsnew/v2.3.3.rst index 1184835ff3a1a..3df86e15baf95 100644 --- a/doc/source/whatsnew/v2.3.3.rst +++ b/doc/source/whatsnew/v2.3.3.rst @@ -25,7 +25,7 @@ Bug fixes - Fix bug in :meth:`Series.str.replace` using named capture groups (e.g., ``\g``) with the Arrow-backed dtype would raise an error (:issue:`57636`) - Fix regression in ``~Series.str.contains``, ``~Series.str.match`` and ``~Series.str.fullmatch`` with a compiled regex and custom flags (:issue:`62240`) -- Fix :meth:`Series.str.fullmatch` not matching patterns with groups correctly for the Arrow-backed string dtype (:issue:`61072`) +- Fix :meth:`Series.str.match` and :meth:`Series.str.fullmatch` not matching patterns with groups correctly for the Arrow-backed string dtype (:issue:`61072`) .. --------------------------------------------------------------------------- .. _whatsnew_233.contributors: diff --git a/pandas/core/arrays/_arrow_string_mixins.py b/pandas/core/arrays/_arrow_string_mixins.py index f6867934327b0..5da94b5987cfc 100644 --- a/pandas/core/arrays/_arrow_string_mixins.py +++ b/pandas/core/arrays/_arrow_string_mixins.py @@ -317,7 +317,7 @@ def _str_match( na: Scalar | lib.NoDefault = lib.no_default, ): if not pat.startswith("^"): - pat = f"^{pat}" + pat = f"^({pat})" return self._str_contains(pat, case, flags, na, regex=True) def _str_fullmatch( diff --git a/pandas/tests/strings/test_find_replace.py b/pandas/tests/strings/test_find_replace.py index c0dbe9cf8ac5f..14c704f4dd1e5 100644 --- a/pandas/tests/strings/test_find_replace.py +++ b/pandas/tests/strings/test_find_replace.py @@ -977,6 +977,30 @@ def test_match_compiled_regex(any_string_dtype): values.str.match(re.compile("ab"), flags=re.IGNORECASE) +@pytest.mark.parametrize( + "pat, case, exp", + [ + ["ab", False, [True, False]], + ["Ab", True, [False, False]], + ["bc", True, [False, False]], + ["a[a-z]{1}", False, [True, False]], + ["A[a-z]{1}", True, [False, False]], + # https://github.com/pandas-dev/pandas/issues/61072 + ["(bc)|(ab)", True, [True, False]], + ["((bc)|(ab))", True, [True, False]], + ], +) +def test_str_match_extra_cases(any_string_dtype, pat, case, exp): + ser = Series(["abc", "Xab"], dtype=any_string_dtype) + result = ser.str.match(pat, case=case) + + expected_dtype = ( + np.bool_ if is_object_or_nan_string_dtype(any_string_dtype) else "boolean" + ) + expected = Series(exp, dtype=expected_dtype) + tm.assert_series_equal(result, expected) + + # -------------------------------------------------------------------------------------- # str.fullmatch # -------------------------------------------------------------------------------------- @@ -1112,7 +1136,6 @@ def test_str_fullmatch_extra_cases(any_string_dtype, pat, case, na, exp): expected_dtype = ( "object" if is_object_or_nan_string_dtype(any_string_dtype) else "boolean" ) - expected = Series([True, False, np.nan, False], dtype=expected_dtype) expected = Series(exp, dtype=expected_dtype) tm.assert_series_equal(result, expected)