From 549fffff150a70a62e7185e045dc1e5e454ffa5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20H=C3=B6rsch?= Date: Mon, 20 May 2024 08:25:58 +0200 Subject: [PATCH] fix(utils): Fix shell_pattern_to_regex to work with brackets (#56) * fix(utils): Fix shell_pattern_to_regex to work with brackets * Update CHANGELOG --- CHANGELOG.rst | 1 + src/pandas_indexing/utils.py | 14 ++------------ 2 files changed, 3 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 117eb85..73776e4 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,7 @@ Changelog ========= +* Fix :func:`~selectors.ismatch` to not interpret brackets as regex symbols * Fix :func:`~selectors.isin` to always return a series (instead of a numpy array) v0.5.0 (2024-04-09) diff --git a/src/pandas_indexing/utils.py b/src/pandas_indexing/utils.py index 6b1fdd7..7bdbcd8 100644 --- a/src/pandas_indexing/utils.py +++ b/src/pandas_indexing/utils.py @@ -3,6 +3,7 @@ Simple utility functions not of greater interest """ +import re from typing import Union from pandas import DataFrame, Index, Series @@ -15,18 +16,7 @@ def shell_pattern_to_regex(s): """ Escape characters with specific regexp use. """ - return ( - str(s) - .replace("|", r"\|") - .replace(".", r"\.") # `.` has to be replaced before `*` - .replace("**", "__starstar__") # temporarily __starstar__ - .replace("*", r"[^|]*") - .replace("__starstar__", r".*") - .replace("+", r"\+") - .replace("(", r"\(") - .replace(")", r"\)") - .replace("$", r"\$") - ) + return re.escape(s).replace(r"\*\*", ".*").replace(r"\*", r"[^|]*") def print_list(x, n):