Skip to content

regexfilters

Class info

🛈 DocStrings

re_escape

re_escape(string: str, re_type: Literal['python', 'posix_basic'] = 'python') -> str

Escape all regular expressions special characters from STRING.

Filter adapted from Ansible

Parameters:

Name Type Description Default
string str

The string to escape

required
re_type Literal['python', 'posix_basic']

The escape type

'python'
Source code in src/jinjarope/regexfilters.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def re_escape(string: str, re_type: Literal["python", "posix_basic"] = "python") -> str:
    """Escape all regular expressions special characters from STRING.

    Filter adapted from Ansible

    Args:
        string: The string to escape
        re_type: The escape type
    """
    match re_type:
        case "python":
            return re.escape(string)
        case "posix_basic":
            # list of BRE special chars:
            return re_replace(string, r"([].[^$*\\])", r"\\\1")
        case _:
            msg = f"Invalid regex type ({re_type})"
            raise NotImplementedError(msg)

re_findall

re_findall(
    value: str, regex: str, *, ignorecase: bool = False, multiline: bool = False
) -> list[Any]

Perform re.findall and return the list of matches.

Filter adapted from Ansible

Parameters:

Name Type Description Default
value str

The text to search in

required
regex str

The regex to use for searching

required
ignorecase bool

Whether char casing should be ignored

False
multiline bool

Whether to perform a multi-line search

False
Source code in src/jinjarope/regexfilters.py
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def re_findall(
    value: str,
    regex: str,
    *,
    ignorecase: bool = False,
    multiline: bool = False,
) -> list[Any]:
    """Perform re.findall and return the list of matches.

    Filter adapted from Ansible

    Args:
        value: The text to search in
        regex: The regex to use for searching
        ignorecase: Whether char casing should be ignored
        multiline: Whether to perform a multi-line search
    """
    flags = 0
    if ignorecase:
        flags |= re.I
    if multiline:
        flags |= re.M
    return re.findall(regex, value, flags)

re_replace

re_replace(
    value: str = "",
    pattern: str = "",
    replacement: str = "",
    *,
    ignorecase: bool = False,
    multiline: bool = False,
    count: int = 0
) -> str

Perform a re.sub returning a string.

Filter adapted from Ansible

Parameters:

Name Type Description Default
value str

The value to search-replace.

''
pattern str

The regex pattern to use

''
replacement str

The replacement pattern to use

''
ignorecase bool

Whether to ignore casing

False
multiline bool

Whether to do a multiline regex search

False
count int

Amount of maximum substitutes.

0
Source code in src/jinjarope/regexfilters.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def re_replace(
    value: str = "",
    pattern: str = "",
    replacement: str = "",
    *,
    ignorecase: bool = False,
    multiline: bool = False,
    count: int = 0,
) -> str:
    """Perform a `re.sub` returning a string.

    Filter adapted from Ansible

    Args:
        value: The value to search-replace.
        pattern: The regex pattern to use
        replacement: The replacement pattern to use
        ignorecase: Whether to ignore casing
        multiline: Whether to do a multiline regex search
        count: Amount of maximum substitutes.
    """
    flags = 0
    if ignorecase:
        flags |= re.I
    if multiline:
        flags |= re.M
    pat = re.compile(pattern, flags=flags)
    output, _subs = pat.subn(replacement, value, count=count)
    return output
re_search(
    value: str, regex: str, *args: str, ignorecase: bool = False, multiline: bool = False
) -> list[str] | None | str

Perform re.search and return the list of matches or a backref.

Filter adapted from Ansible

Parameters:

Name Type Description Default
value str

The text to search in

required
regex str

The regex to use for searching

required
args str

Optional back references to return

()
ignorecase bool

Whether char casing should be ignored

False
multiline bool

Whether to perform a multi-line search

False
Source code in src/jinjarope/regexfilters.py
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def re_search(
    value: str,
    regex: str,
    *args: str,
    ignorecase: bool = False,
    multiline: bool = False,
) -> list[str] | None | str:
    """Perform re.search and return the list of matches or a backref.

    Filter adapted from Ansible

    Args:
        value: The text to search in
        regex: The regex to use for searching
        args: Optional back references to return
        ignorecase: Whether char casing should be ignored
        multiline: Whether to perform a multi-line search
    """
    groups = list()
    for arg in args:
        if arg.startswith("\\g"):
            if match := re.match(r"\\g<(\S+)>", arg):
                groups.append(match.group(1))
        elif arg.startswith("\\"):
            if match := re.match(r"\\(\d+)", arg):
                groups.append(int(match.group(1)))
        else:
            msg = "Unknown argument"
            raise FilterArgumentError(msg)

    flags = 0
    if ignorecase:
        flags |= re.I
    if multiline:
        flags |= re.M
    if match := re.search(regex, value, flags):
        if not groups:
            return match.group()
        return [match.group(item) for item in groups]
    return None