Skip to content

Reference

make

make(
    words: Sequence[str] | Sequence[Tuple[str, ...]],
    prefix: str = "\\b",
    suffix: str = "\\b",
)

Create a string that represents a regular expression object from a set of strings

Parameters:

Name Type Description Default
words Sequence[str]|Sequence[tuple[str, ...]

Sequence or set of strings to be made into a regex. Tuples are also accepted in order to support escaping of special characters.

required
prefix str

Left delimiter for pattern

'\\b'
suffix str

Right delimiter for pattern

'\\b'

Returns:

Type Description
string

A string representing a regular expression pattern

Examples:

>>> import re
>>> import trrex as tx
>>> pattern = tx.make(["baby", "bat", "bad"])
>>> re.findall(pattern, "The baby was scared by the bad bat.")
['baby', 'bad', 'bat']
Source code in trrex/trrex.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def make(words: Sequence[str]|Sequence[Tuple[str, ...]], prefix: str = r"\b", suffix: str = r"\b"):
    """Create a string that represents a regular expression object from a set of strings

    Parameters
    ----------
    words : Sequence[str]|Sequence[tuple[str, ...]
        Sequence or set of strings to be made into a regex.  Tuples are also
        accepted in order to support escaping of special characters.

    prefix : str, optional
           Left delimiter for pattern

    suffix : str, optional
           Right delimiter for pattern

    Returns
    -------
    : string
            A string representing a regular expression pattern

    Examples
    --------

    >>> import re
    >>> import trrex as tx
    >>> pattern = tx.make(["baby", "bat", "bad"])
    >>> re.findall(pattern, "The baby was scared by the bad bat.")
    ['baby', 'bad', 'bat']
    """

    return _Trie(words, left=prefix, right=suffix).make()