From 005e97f0347b60422b17b5286619cb9a1017b708 Mon Sep 17 00:00:00 2001 From: Cliff Hill Date: Wed, 30 Oct 2024 09:09:53 -0400 Subject: [PATCH] Using re.compile(). Signed-off-by: Cliff Hill --- notifications_utils/logging.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/notifications_utils/logging.py b/notifications_utils/logging.py index a78d3dd75..f2ed80e71 100644 --- a/notifications_utils/logging.py +++ b/notifications_utils/logging.py @@ -18,12 +18,14 @@ TIME_FORMAT = "%Y-%m-%dT%H:%M:%S" logger = logging.getLogger(__name__) +_phone_regex = re.compile("(?:\\+ *)?\\d[\\d\\- ]{7,}\\d") + def _scrub(msg: Any) -> Any: # Sometimes just an exception object is passed in for the message, skip those. if not isinstance(msg, str): return msg - phones = re.findall("(?:\\+ *)?\\d[\\d\\- ]{7,}\\d", msg) + phones = _phone_regex.findall(msg) phones = [phone.replace("-", "").replace(" ", "") for phone in phones] for phone in phones: msg = msg.replace(phone, "1XXXXXXXXXX")