-
-
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\"."
match = regex.match(pattern, text):For this example you would be able to refer to the matched groups as follows:
-
match.group(0)is'single quotes' -
match.group('quote')is' -
match.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))."
match = regex.findall(pattern, text)Here, match would be a list:
[
"(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]]."
match = regex.findall(pattern, text)Here, match would be a list:
[
"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)")'
match = regex.findall(pattern, text)Here, match would be a list:
[
"(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)."
match = regex.findall(pattern, text)Here, match would be a list:
[
"( spaced content )",
"(regular content)"
]... WIP ...
★⠀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