-
-
Notifications
You must be signed in to change notification settings - Fork 0
regex
This class includes methods that dynamically generate templates for long, complex regex patterns.
Generates a regex pattern, that matches pairs of quotes.
Params: no params
Returns: the generated regex pattern (Attention: Requires non-standard library regex, not standard library re!)
The quotes() pattern includes two named groups quote and string, which can be used to extract the type of quote and the content inside the quotes, respectively.
Example:
import regex
pattern = Regex.quotes()
text = "Some text with 'single quotes' and \"double quotes\"."
matches = regex.match(pattern, text):For this example you would be able to refer to the matched groups as follows:
-
matches.group(0)is'single quotes' -
matches.group('quote')is' -
matches.group('string')issingle quotes
Generates a regex pattern, that matches everything inside pairs of brackets, including other nested brackets.
Params:
-
bracket1: str = "("the opening bracket -
bracket2: str = ")"the closing bracket -
is_group: bool = Falsewhether to make the bracket content a group or not -
strip_spaces: bool = Truewhether to strip spaces from the content of the brackets or not -
ignore_in_strings: bool = Truewhether to ignore the closing bracket if it inside quotes (strings) or not
Returns: the generated regex pattern (Attention: Requires non-standard library regex, not standard library re!)
Example:
Matching the default brackets (), including the brackets:
pattern = Regex.brackets()
text = "This (is a test) with (nested (brackets))."
matches = regex.findall(pattern, text)Here, matches would be:
[
"(is a test)",
"(nested (brackets))",
"(brackets)"
]Matching custom brackets [] and treating the content as a group:
pattern = Regex.brackets(bracket1="[", bracket2="]", is_group=True)
text = "List items [item1] and [item2 [nested item]]."
matches = regex.findall(pattern, text)Here, matches would be:
[
"item1",
"item2 [nested item]",
"nested item"
]Matching brackets while ignoring closing brackets inside strings:
pattern = Regex.brackets(ignore_in_strings=False)
text = 'func(param = "f(x)")'
matches = regex.findall(pattern, text)Here, matches would be:
[
"(param = \"f(x)\")"
]Matching brackets without stripping spaces of the content:
pattern = Regex.brackets(strip_spaces=False)
text = "Text with ( spaced content ) and (regular content)."
matches = regex.findall(pattern, text)Here, matches would be:
[
"( spaced content )",
"(regular content)"
]Generates a regex pattern that matches the given pattern only when it is not found inside a string ('...' or "...").
Param: pattern: str = r".*" the pattern to match outside strings
Returns: the generated regex pattern
Example:
Matching numbers that are not inside strings:
import re
pattern = Regex.outside_strings(r"\d+")
text = 'Number 123 and "string 456" and 789'
matches = re.findall(pattern, text)Here, matches would be:
[
"123",
"789"
]The number 456 is not matched because it's inside a string.
Generates a regex pattern that matches everything except a specified pattern, unless the pattern is found inside a string ('...' or "...").
Params:
-
disallowed_pattern: strthe pattern that should not be matched -
ignore_pattern: str = ""a pattern that is always ignored (useful for exceptions) -
is_group: bool = Falsewhether to make the matched content a group or not
Returns: the generated regex pattern
Example:
Matching everything until the disallowed pattern > is hit:
import re
pattern = Regex.all_except(">")
text = "Hello > World"
matches = re.match(pattern, text)Here, matches would be:
[
"Hello "
]Using the ignore_pattern parameter to allow a certain pattern, even though it contains the disallowed pattern:
import re
pattern = Regex.all_except(">", "->")
text = "Arrow -> and greater > sign"
match = re.match(pattern, text)Here, the -> arrow is allowed even though it contains >, so matches would be:
[
"Arrow -> and greater "
]Generates a regex pattern that matches function calls and returns two groups: the function name and its arguments.
Param: func_name: Optional[str] = None the specific function name to match (if None, matches any function)
Returns: the generated regex pattern (Attention: Requires non-standard library regex, not standard library re!)
The func_call() pattern returns two groups:
- The function name
- The function's arguments (content inside the parentheses)
Example:
Matching any function call:
import regex
pattern = Regex.func_call()
text = "Call print('hello') and input('prompt')"
matches = regex.findall(pattern, text)Here, matches would be:
[
("print", "'hello'"),
("input", "'prompt'")
]Matching a specific function:
import regex
pattern = Regex.func_call("print")
text = "Call print('hello') and input('prompt') and print('world')"
matches = regex.findall(pattern, text)Here, matches would be:
[
("print", "'hello'"),
("print", "'world'")
]Generates a regex pattern that matches RGBA color values inside a string.
Params:
-
fix_sep: str = ","the separator between color values (if empty, any non-alphanumeric char can be used) -
allow_alpha: bool = Truewhether to allow the alpha (opacity) channel or not
Returns: the generated regex pattern
The RGBA color can be in the formats (for fix_sep = ','):
rgba(r, g, b)-
rgba(r, g, b, a)(ifallow_alpha=True) (r, g, b)-
(r, g, b, a)(ifallow_alpha=True) r, g, b-
r, g, b, a(ifallow_alpha=True)
Valid ranges:
-
r0-255 (int: red) -
g0-255 (int: green) -
b0-255 (int: blue) -
a0.0-1.0 (float: opacity)
Example:
Matching RGBA colors with default separator:
import re
pattern = Regex.rgba_str()
text = "Color rgba(255, 128, 0) and (100, 200, 50, 0.5)"
matches = re.findall(pattern, text)Here, matches would be:
[
("255", "128", "0", ""),
("100", "200", "50", "0.5")
]Matching RGB colors without alpha channel:
import re
pattern = 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)Here, matches would be:
[
("255", "128", "0"),
("255", "0", "0")
]Using a custom separator:
import re
pattern = Regex.rgba_str(fix_sep="|")
text = "Color 255|128|0"
matches = re.findall(pattern, text)Here, matches would be:
[
("255", "128", "0", "")
]Generates a regex pattern that matches HSLA color values inside a string.
Params:
-
fix_sep: str = ","the separator between color values (if empty, any non-alphanumeric char can be used) -
allow_alpha: bool = Truewhether to allow the alpha (opacity) channel or not
Returns: the generated regex pattern
The HSLA color can be in the formats (for fix_sep = ','):
hsla(h, s, l)-
hsla(h, s, l, a)(ifallow_alpha=True) (h, s, l)-
(h, s, l, a)(ifallow_alpha=True) h, s, l-
h, s, l, a(ifallow_alpha=True)
Valid ranges:
-
h0-360 (int: hue, optional trailing degree°sign) -
s0-100 (int: saturation, optional trailing percent%sign) -
l0-100 (int: lightness, optional trailing percent%sign) -
a0.0-1.0 (float: opacity)
Example:
Matching HSLA colors with default separator:
import re
pattern = Regex.hsla_str()
text = "Color hsla(240°, 100%, 50%) and (120, 80, 60, 0.8)"
matches = re.findall(pattern, text)Here, matches would be:
[
("240", "100", "50", ""),
("120", "80", "60", "0.8")
]Matching HSL colors without alpha channel:
import re
pattern = 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)Here, matches would be:
[
("240", "100", "50"),
("360", "100", "50")
]Using a custom separator:
import re
pattern = Regex.hsla_str(fix_sep=" ")
text = "Color 240 100% 50%"
matches = re.findall(pattern, text)Here, matches would be:
[
("240", "100", "50", "")
]Generates a regex pattern that matches hexadecimal color values inside a string.
Param: allow_alpha: bool = True whether to allow the alpha (opacity) channel or not
Returns: the generated regex pattern
The HEXA color can be in the formats (with optional # or 0x prefix):
-
RGB(3-digit short form) -
RGBA(4-digit short form, ifallow_alpha=True) -
RRGGBB(6-digit full form) -
RRGGBBAA(8-digit full form, ifallow_alpha=True)
Valid ranges:
- Every channel from 0-9 and A-F (case-insensitive)
Example:
Matching HEXA colors with alpha channel:
import re
pattern = Regex.hexa_str()
text = "Colors: #FF0000, 0xABCDEF, F00 and FF000080"
matches = re.findall(pattern, text)Here, matches would be:
[
"FF0000",
"ABCDEF",
"F00",
"FF0000FF"
]Matching HEXA colors without alpha channel:
import re
pattern = Regex.hexa_str(allow_alpha=False)
text = "Without #FF0000 #F00 and with opacity #FF000080 #F008"
matches = re.findall(pattern, text)Here, matches would be:
[
"FF0000",
"F00",
"FF0000",
"F00"
]★⠀Python Library by XulbuX⠀★
-
Intended Audience:
- Developers
-
License:
- OSI Approved
- MIT License
-
Operating Systems:
- Full Library: OS Independent
-
Supported Python Versions:
- Python 3.13
- Python 3.12
- Python 3.11
- Python 3.10
-
Topics:
- Libraries
- Python Modules
- Software Development