### Install Regex-Toolkit development version from GitHub Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md These commands clone the Regex-Toolkit repository from GitHub and install the development version in editable mode. This is useful for contributing to the project or using the latest unreleased features. ```Bash git clone git+https://github.com/Phosmic/regex-toolkit.git cd regex-toolkit python3 -m pip install -e . ``` -------------------------------- ### Install Regex-Toolkit via pip Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This command installs the stable version of Regex-Toolkit from PyPi using pip. It requires Python 3.10 or higher. ```Bash python3 -m pip install regex-toolkit ``` -------------------------------- ### Get Characters within a Range as Tuple (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Returns a tuple containing all characters within a specified range, inclusive of both the starting and ending characters. Similar to `iter_char_range` but returns a complete collection. Useful for generating fixed character sets for further processing. ```APIDOC char_range: def char_range(first_char: str, last_char: str) -> tuple[str, ...] Arguments: first_char: str - First character (inclusive). last_char: str - Last character (inclusive). Returns: tuple[str, ...] - Characters within a range of characters. ``` ```python import regex_toolkit as rtk rtk.char_range("a", "d") # Output: ('a', 'b', 'c', 'd') rtk.char_range("d", "a") # Output: ('d', 'c', 'b', 'a') rtk.char_range("🐶", "🐺") # Output: ("🐶", "🐷", "🐸", "🐹", "🐺") ``` -------------------------------- ### Iterate Characters within a Range (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Provides an iterator for all characters within a specified range, inclusive of both the starting and ending characters. The iteration can proceed in ascending or descending order based on the character's ordinal value. Useful for generating character sets dynamically. ```APIDOC iter_char_range: def iter_char_range(first_char: str, last_char: str) -> Generator[str, None, None] Arguments: first_char: str - Starting (first) character. last_char: str - Ending (last) character. Yields: str - Characters within a range of characters. ``` ```python import regex_toolkit as rtk tuple(rtk.iter_char_range("a", "c")) # Output: ('a', 'b', 'c') tuple(rtk.iter_char_range("c", "a")) # Output: ('c', 'b', 'a') tuple(rtk.iter_char_range("🐶", "🐺")) # Output: ("🐶", "🐷", "🐸", "🐹", "🐺") ``` -------------------------------- ### Slice and Mask String with Single Span (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Modifies a string by replacing a specified span (start inclusive, end exclusive) with an optional mask string. If no mask is provided, the span is simply removed. This function is useful for targeted string manipulation, such as redaction or insertion. ```APIDOC mask_span: def mask_span(text: str, span: Sequence[int], mask: str | None = None) -> str Arguments: text: str - String to slice. span: Sequence[int] - Span to slice (start is inclusive, end is exclusive). mask: str | None, optional - String to replace the span with. Defaults to None. Returns: str - String with span replaced with the mask text. ``` ```python import regex_toolkit as rtk rtk.mask_span("example", (0, 2)) # Output: 'ample' rtk.mask_span("This is a example", (10, 10), "insert ") # Output: 'This is a insert example' rtk.mask_span("This is a example", (5, 7), "replaces part of") # Output: 'This replaces part of a example' ``` -------------------------------- ### Import Regex-Toolkit and regex modules in Python Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This snippet demonstrates how to import the necessary modules to begin using Regex-Toolkit. It includes Python's standard `re` module, the `re2` module, and `regex_toolkit` itself, aliased as `rtk` for convenience. ```Python import re import re2 import regex_toolkit as rtk ``` -------------------------------- ### Python Project Dependency List Source: https://github.com/phosmic/regex-toolkit/blob/main/requirements-test.txt This snippet specifies the required Python packages and their minimum versions for the project. It includes core testing frameworks like pytest, a code coverage tool, a distributed testing plugin, and the google-re2 library for regular expressions. ```Python Requirements pytest>=7.0.0 pytest-cov pytest-xdist>=2.2.0 google-re2>=1.0 ``` -------------------------------- ### string_as_exp: Create Regex for Exact String Match Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This function constructs a regular expression that exactly matches a specified string. It handles escaping special characters within the string and allows for selection of different regex flavors. The function is useful for creating precise string matching patterns. ```APIDOC string_as_exp(text: str, flavor: int | None = None) -> str text: String to match. flavor: Regex flavor (1 for RE, 2 for RE2). Defaults to None. Returns: Expression that exactly matches the original string. Raises: ValueError: Invalid regex flavor. ``` ```python import regex_toolkit as rtk rtk.string_as_exp("http://www.example.com") # Output: 'https\:\/\/example\.com' ``` ```python import regex_toolkit as rtk rtk.string_as_exp("http://www.example.com", flavor=2) # Output: 'https\x{003a}\x{002f}\x{002f}example\.com' ``` -------------------------------- ### make_exp: Create Regex for Character Set Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This function creates a regular expression that matches any character from a given list of characters. It intelligently sorts and groups characters into ranges (e.g., 'a-c') where possible, optimizing the resulting regex. The expression is not anchored, allowing it to be integrated into larger patterns. ```APIDOC make_exp(chars: Iterable[str], flavor: int | None = None) -> str chars: Characters to match. flavor: Regex flavor (1 for RE, 2 for RE2). Defaults to None. Returns: Expression that exactly matches the original characters. Raises: ValueError: Invalid regex flavor. ``` ```python import regex_toolkit as rtk "[" + rtk.make_exp(["a", "b", "c", "z", "y", "x"]) + "]" # Output: '[a-cx-z]' ``` ```python import regex_toolkit as rtk "[" + rtk.make_exp(["a", "b", "c", "z", "y", "x"], flavor=2) + "]" # Output: '[a-cx-z]' ``` -------------------------------- ### strings_as_exp: Create Regex for Any of Multiple Strings Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This function generates a regular expression that matches any one of a provided list of strings. It effectively creates an 'OR' pattern for multiple string options, handling character escaping and supporting different regex flavors. This is useful for matching against a predefined set of keywords or phrases. ```APIDOC strings_as_exp(texts: Iterable[str], flavor: int | None = None) -> str texts: Strings to match. flavor: Regex flavor (1 for RE, 2 for RE2). Defaults to None. Returns: Expression that exactly matches any one of the original strings. Raises: ValueError: Invalid regex flavor. ``` ```python import regex_toolkit as rtk rtk.strings_as_exp(["apple", "banana", "cherry"]) # Output: 'banana|cherry|apple' ``` ```python import regex_toolkit as rtk rtk.strings_as_exp(["apple", "banana", "cherry"], flavor=2) # Output: 'banana|cherry|apple' ``` -------------------------------- ### Convert Character to Codepoint String (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Converts a given character to its hexadecimal codepoint representation. It supports zero-padding to a specified length, defaulting to 8 characters, or no padding. This utility is useful for Unicode character analysis and consistent representation. ```APIDOC char_to_cpoint: def char_to_cpoint(char: str, *, zfill: int | None = 8) -> str Arguments: char: str - Character. zfill: int | None, optional - Amount of characters to zero-pad the codepoint to. Defaults to 8. Returns: str - Character codepoint. ``` ```python import regex_toolkit as rtk rtk.char_to_cpoint("🐶") # Output: '0001F436' # Disable zero-padding by setting `zfill` to `0` or `None`. rtk.char_to_cpoint("🐶", zfill=0) # Output: '1F436' ``` -------------------------------- ### Escape Character for Regex Expression (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Creates a regex expression that precisely matches a given character, handling special regex characters by escaping them. An optional `flavor` parameter might influence the escaping behavior for different regex engines. Essential for constructing safe regex patterns from arbitrary strings. ```APIDOC escape: def escape(char: str, flavor: int | None = None) -> str Arguments: char: str - Character. flavor: int | None, optional - (No description provided). Returns: str - Regex expression for the character. ``` ```python import regex_toolkit as rtk rtk.escape("a") # Output: 'a' rtk.escape(".") # Output: '\.' rtk.escape("/") # Output: '/' rtk.escape(".", flavor=2) # Output: '\.' rtk.escape("a", flavor=2) # Output: 'a' rtk.escape("/", flavor=2) ``` -------------------------------- ### char_as_exp: Create Regex for Single Character Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md This function generates a regular expression that precisely matches a given single character. It supports different regex flavors (RE or RE2) and raises errors for invalid inputs. The output is an escaped string suitable for use in a regex pattern. ```APIDOC char_as_exp(char: str, flavor: int | None = None) -> str char: Character to match. flavor: Regex flavor (1 for RE, 2 for RE2). Defaults to None. Returns: Expression that exactly matches the original character. Raises: ValueError: Invalid regex flavor. TypeError: Invalid type for char. ``` ```python import regex_toolkit as rtk rtk.char_as_exp("/") # Output: '\x{002f}' ``` -------------------------------- ### Convert Codepoint to Character Ordinal (APIDOC) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Documents the arguments and return type for a function that converts a character codepoint string to its integer ordinal value. This function is likely used for internal character representation within the `regex_toolkit` library. ```APIDOC Function: (unnamed, inferred) Arguments: cpoint: str - Character codepoint. Returns: int - Character ordinal. ``` -------------------------------- ### API: Convert ordinal to codepoint in regex_toolkit.utils Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Documents the `ord_to_cpoint` function, which converts a character's integer ordinal value into its hexadecimal codepoint string representation. It supports optional zero-padding for consistent output length. ```APIDOC regex_toolkit.utils.ord_to_cpoint def ord_to_cpoint(ordinal: int, *, zfill: int | None = 8) -> str Character ordinal to character codepoint. Produces a hexadecimal ([0-9A-F]) representation of the ordinal. The default zero-padding is 8 characters, which is the maximum amount of characters in a codepoint. Arguments: - ordinal int - Character ordinal. - zfill int | None, optional - Amount of characters to zero-pad the codepoint to. Defaults to 8. Returns: - str - Character codepoint. ``` ```Python import regex_toolkit as rtk rtk.ord_to_cpoint(128054) # Output: '0001F436' # Disable zero-padding by setting `zfill` to `0` or `None`. rtk.ord_to_cpoint(128054, zfill=0) # Output: '1F436' ``` -------------------------------- ### Normalize Unicode String to NFC Form (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Normalizes a Unicode string to Normalization Form C (NFC). This form favors the use of a single, precomposed character where possible, simplifying string comparisons and ensuring consistent representation of Unicode characters. ```APIDOC to_nfc: def to_nfc(text: str) -> str Arguments: text: str - String to normalize. Returns: str - Normalized string. ``` ```python import regex_toolkit as rtk rtk.to_nfc("é") # Output: 'é' ``` -------------------------------- ### API: Convert codepoint to ordinal in regex_toolkit.utils Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Documents the `cpoint_to_ord` function, which converts a hexadecimal character codepoint string into its integer ordinal value. This is the inverse operation of `ord_to_cpoint`. ```APIDOC regex_toolkit.utils.cpoint_to_ord def cpoint_to_ord(cpoint: str) -> int Character codepoint to character ordinal. Arguments: - cpoint str - Character codepoint. Returns: - int - Character ordinal. ``` ```Python import regex_toolkit as rtk rtk.cpoint_to_ord("0001F436") # Output: 128054 rtk.cpoint_to_ord("1f436") ``` -------------------------------- ### Slice and Mask String with Multiple Spans (Python) Source: https://github.com/phosmic/regex-toolkit/blob/main/README.md Modifies a string by replacing multiple specified spans with corresponding mask strings. This function allows for complex string transformations by applying several replacements simultaneously. It's important to note potential behaviors for overlapping or out-of-order spans. ```APIDOC mask_spans: def mask_spans(text: str, spans: Sequence[Sequence[int]], masks: Sequence[str] | None = None) -> str Arguments: text: str - String to slice. spans: Sequence[Sequence[int]] - Spans to slice (start is inclusive, end is exclusive). masks: Sequence[str], optional - Strings to replace the spans with. Defaults to None. Returns: str - String with all spans replaced with the mask text. ``` ```python import regex_toolkit as rtk rtk.mask_spans( text="This is a example", masks=["replaces part of", "insert "], spans=[(5, 7), (10, 10)], ) # Output: 'This replaces part of a insert example' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.