Package: regex-toolkit
Regex-ToolkitRegex-Toolkitprovides tools for creatingREandRE2expressions.Requirements:Regex-Toolkitrequires Python 3.10 or higher, is platform independent, and has no outside dependencies.Issue reportingIf you discover an issue with Regex-Toolkit, please report it athttps://github.com/Phosmic/regex-toolkit/issues.LicenseThis program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.You should have received a copy of the GNU General Public License along with this program. If not, seehttps://www.gnu.org/licenses/.RequirementsInstallingUsageLibraryInstallingMost stable version fromPyPi:python3-mpipinstallregex-toolkitDevelopment version fromGitHub:gitclonegit+https://github.com/Phosmic/regex-toolkit.gitcdregex-toolkit
python3-mpipinstall-e.UsageTo harness the toolkit's capabilities, you should import the necessary packages:importre# and/orimportre2importregex_toolkitasrtkWhy Useregex_toolkit?Regex definitions vary across languages and versions.
By using the toolkit, you can achieve a more consistent and comprehensive representation of unicode support.
It is especially useful to supplement base unicode sets with the latest definitions from other languages and standards.RE2 OverviewRE2 focuses on safely processing regular expressions, particularly from untrusted inputs.
It ensures both linear match time and efficient memory usage.
Although it might not always surpass other engines in speed, it intentionally omits features that depend solely on backtracking, like backreferences and look-around assertions.A brief rundown of RE2 terminology:BitState: An execution engine that uses backtracking search.bytecode: The set of instructions that form an automaton.DFA: The engine for Deterministic Finite Automaton searches.NFA: Implements the Nondeterministic Finite Automaton search method.OnePass: A one-pass search execution engine.pattern: The textual form of a regex.Prog: The compiled version of a regex.Regexp: The parsed version of a regex.Rune: A character in terms of encoding, essentially a code point.For an in-depth exploration, please refer to theRE2 documentation.Libraryregex_toolkit.utilsresolve_flavordefresolve_flavor(potential_flavor:int|RegexFlavor|None)->RegexFlavorResolve a regex flavor.If the flavor is an integer, it is validated and returned.
If the flavor is a RegexFlavor, it is returned.
If the flavor is None, the default flavor is returned. To change the default flavor, setdefault_flavor.importregex_toolkitasrtkrtk.base.default_flavor=2assertrtk.utils.resolve_flavor(None)==rtk.enums.RegexFlavor.RE2Arguments:potential_flavorint | RegexFlavor | None- Potential regex flavor.Returns:RegexFlavor- Resolved regex flavor.Raises:ValueError- Invalid regex flavor.iter_sort_by_lendefiter_sort_by_len(texts:Iterable[str],*,reverse:bool=False)->Generator[str,None,None]Iterate strings sorted by length.Arguments:textsIterable[str]- Strings to sort.reversebool, optional- Sort in descending order (longest to shortest). Defaults to False.Yields:str- Strings sorted by length.sort_by_lendefsort_by_len(texts:Iterable[str],*,reverse:bool=False)->tuple[str,...]Sort strings by length.Arguments:textsIterable[str]- Strings to sort.reversebool, optional- Sort in descending order (longest to shortest). Defaults to False.Returns:tuple[str, ...]- Strings sorted by length.ord_to_cpointdeford_to_cpoint(ordinal:int)->strCharacter ordinal to character codepoint.The codepoint is always 8 characters long (zero-padded).Example:ord_to_cpoint(97)# Output: '00000061'Arguments:ordinalint- Character ordinal.Returns:str- Character codepoint.cpoint_to_orddefcpoint_to_ord(cpoint:str)->intCharacter codepoint to character ordinal.Arguments:cpointstr- Character codepoint.Returns:int- Character ordinal.char_to_cpointdefchar_to_cpoint(char:str)->strCharacter to character codepoint.Example:char_to_cpoint("a")# Output: '00000061'Arguments:charstr- Character.Returns:str- Character codepoint.to_nfcdefto_nfc(text:str)->strNormalize a Unicode string to NFC form C.Form C favors the use of a fully combined character.Example:to_nfc("e\\u0301")=="é"# Output: TrueArguments:textstr- String to normalize.Returns:str- Normalized string.iter_char_rangedefiter_char_range(first_char:str,last_char:str)->Generator[str,None,None]Iterate all characters within a range of characters (inclusive).Example:char_range("a","c")# Output: ('a', 'b', 'c')char_range("c","a")# Output: ('c', 'b', 'a')Arguments:first_charstr- Starting (first) character.last_charstr- Ending (last) character.Yields:str- Characters within a range of characters.char_rangedefchar_range(first_char:str,last_char:str)->tuple[str,...]Tuple of all characters within a range of characters (inclusive).Example:char_range("a","d")# Output: ('a', 'b', 'c', 'd')char_range("d","a")# Output: ('d', 'c', 'b', 'a')Arguments:first_charstr- Starting (first) character.last_charstr- Ending (last) character.Returns:tuple[str, ...]- Characters within a range of characters.mask_spandefmask_span(text:str,span:list[int]|tuple[int,int],mask:str|None=None)->strSlice and mask a string using a single span.Arguments:textstr- String to slice.spanlist[int] | tuple[int, int]- Domain of index positions (start, end) to mask.maskstr, optional- Mask to insert after slicing. Defaults to None.Returns:str- String with span replaced with the mask text.mask_spansdefmask_spans(text:str,spans:Iterable[list[int]|tuple[int,int]],masks:Iterable[str]|None=None)->strSlice and mask a string using multiple spans.Todo: Add support for overlapping (and unordered?) spans.Arguments:textstr- String to slice.spansIterable[list[int] | tuple[int, int]]- Domains of index positions (x1, x2) to mask within the text.masksIterable[str], optional- Masks to insert when slicing. Defaults to None.Returns:str- String with all spans replaced with the mask text.regex_toolkit.baseescapedefescape(char:str,flavor:int|None=None)->strCreate a regex expression that exactly matches a character.Arguments:charstr- Character to match.flavorint | None, optional- Regex flavor (1 for RE, 2 for RE2). Defaults to None.Returns:str- Expression that exactly matches the original character.Raises:ValueError- Invalid regex flavor.string_as_expdefstring_as_exp(text:str,flavor:int|None=None)->strCreate a regex expression that exactly matches a string.Arguments:textstr- String to match.flavorint | None, optional- Regex flavor (1 for RE, 2 for RE2). Defaults to None.Returns:str- Expression that exactly matches the original string.Raises:ValueError- Invalid regex flavor.strings_as_expdefstrings_as_exp(texts:Iterable[str],flavor:int|None=None)->strCreate a regex expression that exactly matches any one string.Arguments:textsIterable[str]- Strings to match.flavorint | None, optional- Regex flavor (1 for RE, 2 for RE2). Defaults to None.Returns:str- Expression that exactly matches any one of the original strings.Raises:ValueError- Invalid regex flavor.make_expdefmake_exp(chars:Iterable[str],flavor:int|None=None)->strCreate a regex expression that exactly matches a list of characters.The characters are sorted and grouped into ranges where possible.
The expression is not anchored, so it can be used as part of a larger expression.Example:exp="["+make_exp(["a","b","c","z","y","x"])+"]"# Output: '[a-cx-z]'Arguments:charsIterable[str]- Characters to match.flavorint | None, optional- Regex flavor (1 for RE, 2 for RE2). Defaults to None.Returns:str- Expression that exactly matches the original characters.Raises:ValueError- Invalid regex flavor.regex_toolkit.enumsEnums.RegexFlavorObjectsclassRegexFlavor(int,Enum)Regex flavors.Attributes:REint- Standard Python regex flavor.RE2int- Google RE2 regex flavor.