1"""The `suricata_check.utils.regex_provider` module provides a unified interface for regex operations."""
2
3import importlib.util
4import logging
5from typing import TYPE_CHECKING, Union
6
7if TYPE_CHECKING:
8 import re
9
10 import regex
11
12_logger = logging.getLogger(__name__)
13
14# Import the fastest regex provider available:
15if importlib.util.find_spec("regex") is not None:
16 _logger.info("Detected regex module as installed, using it.")
17 import regex as _regex_provider
18else:
19 _logger.warning(
20 """Did not detect regex module as installed, using re instead.
21To increase suricata-check processing speed, consider isntalling the regex module \
22by running `pip install suricata-check[performance]`.""",
23 )
24 import re as _regex_provider
25
26
[docs]
27def get_regex_provider(): # noqa: ANN201
28 """Returns the regex provider to be used.
29
30 If `regex` is installed, it will return that module.
31 Otherwise, it will return the `re` module instead.
32 """
33 return _regex_provider
34
35
36Pattern = Union["re.Pattern", "regex.Pattern"]
37Match = Union["re.Match", "regex.Match"]