Regex Module
Provides utilities for dynamic regex pattern generation and compilation.
Includes composable pattern builders for color strings, balanced brackets,
quotes, and function calls, alongside LazyRegex for deferred compilation.
quotes()
def quotes() -> strMatches pairs of quotes. (strings)
Will create two named groups:
quote– The quote type (single or double).string– Everything inside the found quote pair.
Attention:
Requires non-standard library regex, not standard library re!
Example Usage
import xulbux as xx
import regex
pattern = xx.regex.quotes()
text = "Some text with 'single quotes' and \"double quotes\"."
matches = regex.findall(pattern, text)[
("'", "single quotes"),
('"', "double quotes")
]brackets()
def brackets(
bracket1: str = "(",
bracket2: str = ")",
/,
*,
is_group: bool = False,
strip_spaces: bool = False,
ignore_in_strings: bool = True,
) -> strMatches everything inside pairs of brackets, including other nested brackets.
bracket1– The opening bracket (e.g.,(,{,[, …).bracket2– The closing bracket (e.g.,),},], …).is_group– Whether to create a capturing group for the content inside the brackets.strip_spaces– Whether to strip spaces from the bracket content or not.ignore_in_strings– Whether to ignore closing brackets that are inside strings/quotes (e.g.,'…)…'or"…)…").
Attention:
Requires non-standard library regex, not standard library re!
Example Usages
Default brackets:
import xulbux as xx
import regex
pattern = xx.regex.brackets()
text = "This (is a test) with (nested (brackets))."
# Use `overlapped=True` to extract nested overlapping brackets:
matches = regex.findall(pattern, text, overlapped=True)[
"(is a test)",
"(nested (brackets))",
"(brackets)"
]Custom brackets:
import xulbux as xx
import regex
pattern = xx.regex.brackets("[", "]", is_group=True)
text = "List of items: [item1, item2 [nested item]]"
matches = regex.findall(pattern, text, overlapped=True)[
"item1, item2 [nested item]",
"nested item"
]Without ignoring strings:
import xulbux as xx
import regex
pattern = xx.regex.brackets(ignore_in_strings=False)
text = 'func(param = "f(x)")'
matches = regex.findall(pattern, text, overlapped=True)[
"(param = "f(x)")",
"(x)"
]Without stripping spaces:
import xulbux as xx
import regex
pattern = xx.regex.brackets(strip_spaces=False)
text = " ( spaced content ) and (regular content) "
matches = regex.findall(pattern, text)[
"( spaced content )",
"(regular content)"
]outside_strings()
def outside_strings(pattern: str = r".*", /) -> strMatches the pattern only when it is not found inside a string ('…' or "…").
pattern– The pattern to match outside of strings/quotes.
Attention:
Requires non-standard library regex, not standard library re!
Example Usage
import xulbux as xx
import regex
pattern = xx.regex.outside_strings(r"\d+")
text = 'Number 123 and "string 456" and 789'
matches = regex.findall(pattern, text)[
"123",
"789"
]all_except()
def all_except(disallowed_pattern: str, /, ignore_pattern: str = "", *, is_group: bool = False) -> strMatches everything up to the disallowed_pattern, unless the
disallowed_pattern is found inside a string/quotes ('…' or "…").
disallowed_pattern– The pattern that is not allowed to be matched.ignore_pattern– A pattern that, if found, will make the regex ignore thedisallowed_pattern(even if it contains thedisallowed_patterninside it):
For example ifdisallowed_patternis>andignore_patternis->, the->-arrows will be allowed, even though they have>in them.is_group– Whether to create a capturing group for the matched content.
Attention:
Requires non-standard library regex, not standard library re!
Example Usages
Single exclusion:
import xulbux as xx
import regex
pattern = xx.regex.all_except(">")
text = "Hello > World"
matches = regex.match(pattern, text)[
"Hello "
]Multiple exclusions:
import xulbux as xx
import regex
pattern = xx.regex.all_except(">", "->")
text = "Arrow -> and greater > sign"
match = regex.match(pattern, text)[
"Arrow -> and greater "
]func_call()
def func_call(func_name: str | None = None, /) -> strMatch a function call in code, including its arguments.
func_name– The name of the function to match. IfNone, it will match any function call.
Will create two groups:
- The function name (or any function name if
func_nameisNone). - Everything inside the function call's parentheses (the arguments).
Attention:
Requires non-standard library regex, not standard library re!
Example Usages
Any function:
import xulbux as xx
import regex
pattern = xx.regex.func_call()
text = "Call print('hello') and input('prompt')"
matches = regex.findall(pattern, text)[
("print", "'hello'"),
("input", "'prompt'")
]Specific function:
import xulbux as xx
import regex
pattern = xx.regex.func_call("input")
text = "Call print('hello') and input('prompt') and print('world')"
matches = regex.findall(pattern, text)[
("input", "'prompt'")
]rgba_str()
def rgba_str(fix_sep: str | None = ",", *, allow_alpha: bool = True) -> strMatches an RGBA color inside a string.
fix_sep– The fixed separator between the RGBA values (e.g.,,,;…):
If set to nothing orNone, any char that is not a letter or number can be used to separate the RGBA values, including just a space.allow_alpha– Whether to include the alpha channel in the match.
Valid Formats
With fix_sep = ',', the RGBA color can be in the formats:
rgba(red, green, blue)rgba(red, green, blue, alpha)(ifallow_alpha=True)(red, green, blue)(red, green, blue, alpha)(ifallow_alpha=True)red, green, bluered, green, blue, alpha(ifallow_alpha=True)
Valid Ranges
red0-255 (int: red)green0-255 (int: green)blue0-255 (int: blue)alpha0.0-1.0 (float: opacity)
Example Usages
Default pattern:
import xulbux as xx
import re
pattern = xx.regex.rgba_str()
text = "Color rgba(255, 128, 0) and (100, 200, 50, 0.5)"
matches = re.findall(pattern, text)[
("255", "128", "0", ""),
("100", "200", "50", "0.5")
]No alpha allowed:
import xulbux as xx
import re
pattern = xx.regex.rgba_str(allow_alpha=False)
text = "Color with rgb(255, 128, 0, 0.5) and without opacity rgb(255, 0, 0)"
matches = re.findall(pattern, text)[
("255", "128", "0"),
("255", "0", "0")
]Custom separator:
import xulbux as xx
import re
pattern = xx.regex.rgba_str(fix_sep="|")
text = "Color 255|128|0"
matches = re.findall(pattern, text)[
("255", "128", "0", "")
]hsla_str()
def hsla_str(fix_sep: str | None = ",", *, allow_alpha: bool = True) -> strMatches a HSLA color inside a string.
fix_sep– The fixed separator between the HSLA values (e.g.,,,;…):
If set to nothing orNone, any char that is not a letter or number can be used to separate the HSLA values, including just a space.allow_alpha– Whether to include the alpha channel in the match.
Valid Formats
With fix_sep = ',', the HSLA color can be in the formats:
hsla(hue, sat, light)hsla(hue, sat, light, alpha)(ifallow_alpha=True)(hue, sat, light)(hue, sat, light, alpha)(ifallow_alpha=True)hue, sat, lighthue, sat, light, alpha(ifallow_alpha=True)
Valid Ranges
hue0-360 (int: hue)sat0-100 (int: saturation)light0-100 (int: lightness)alpha0.0-1.0 (float: opacity)
Example Usages
Default pattern:
import xulbux as xx
import re
pattern = xx.regex.hsla_str()
text = "Color hsla(240°, 100%, 50%) and (120, 80, 60, 0.8)"
matches = re.findall(pattern, text)[
("240", "100", "50", ""),
("120", "80", "60", "0.8")
]No alpha allowed:
import xulbux as xx
import re
pattern = xx.regex.hsla_str(allow_alpha=False)
text = "Color with hsl(240, 100%, 50%, 0.5) and without opacity hsl(360, 100%, 50%)"
matches = re.findall(pattern, text)[
("240", "100", "50"),
("360", "100", "50")
]Custom separator:
import xulbux as xx
import re
pattern = xx.regex.hsla_str(fix_sep=" ")
text = "Color 240 100% 50%"
matches = re.findall(pattern, text)[
("240", "100", "50", "")
]hexa_str()
def hexa_str(*, allow_alpha: bool = True) -> strMatches a hex color inside a string.
allow_alpha– Whether to include the alpha channel in the match.
Valid Formats
The hex color can be in the formats (prefix #, 0x or no prefix):
RGBRGBA(ifallow_alpha=True)RRGGBBRRGGBBAA(ifallow_alpha=True)
Valid Ranges
Every channel from 0-9 and A-F (case insensitive)
Example Usages
Default pattern:
import xulbux as xx
import re
pattern = xx.regex.hexa_str()
text = "Colors: #FF0000, 0xABCDEF, F00 and FF000080"
matches = re.findall(pattern, text)[
"FF0000",
"ABCDEF",
"F00",
"FF0000FF"
]No alpha allowed:
import xulbux as xx
import re
pattern = xx.regex.hexa_str(allow_alpha=False)
text = "Without #FF0000 #F00 and with opacity #FF000080 #F008"
matches = re.findall(pattern, text)[
"FF0000",
"F00",
"FF0000",
"F00"
]LazyRegex
class LazyRegex(self, **patterns: str)A class that lazily compiles and caches regex patterns on first access.
**patterns– Keyword arguments where the key is the name of the pattern and the value is the regex pattern string to compile.
Attention:
Requires non-standard library regex, not standard library re!
Example Usage
PATTERNS = LazyRegex(
email=r"(?i)[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}",
phone=r"\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}",
)
# Accessing `email` compiles and caches it for future use:
match = PATTERNS.email.match("test@example.com")
# Accessing `phone` compiles and caches it for future use:
match = PATTERNS.phone.fullmatch("+1 (555) 123-4567")