string_regex¶
shortfx.fxString.string_regex
¶
String Regular Expression Functions.
This module provides high-level regex wrappers for common pattern matching, extraction, and search operations on strings.
Key Features: - Regex pattern matching (boolean) - Single and multi-match extraction - Regex-based splitting
Functions¶
regex_count(text: str, pattern: str, case_sensitive: bool = True) -> int
¶
Counts the number of non-overlapping matches of a regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to search. |
required |
pattern
|
str
|
The regex pattern to count. |
required |
case_sensitive
|
bool
|
If False, performs case-insensitive matching. |
True
|
Returns:
| Type | Description |
|---|---|
int
|
The number of matches found. |
Raises:
| Type | Description |
|---|---|
error
|
If the pattern is not a valid regex. |
Example
regex_count("aAbBaA", r"a", case_sensitive=False) 4 regex_count("one 1 two 2 three 3", r"\d+") 3
Complexity: O(n*m)
Source code in shortfx/fxString/string_regex.py
regex_extract(text: str, pattern: str, group: int = 0, case_sensitive: bool = True) -> Optional[str]
¶
Extracts the first match of a regex pattern from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to search. |
required |
pattern
|
str
|
The regex pattern to find. |
required |
group
|
int
|
The capture group index to return (0 = full match). |
0
|
case_sensitive
|
bool
|
If False, performs case-insensitive matching. |
True
|
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The matched text, or None if no match was found. |
Raises:
| Type | Description |
|---|---|
error
|
If the pattern is not a valid regex. |
Example
regex_extract("Order #12345 confirmed", r"#(\d+)", group=1) '12345' regex_extract("no match here", r"\d+")
Complexity: O(n*m)
Source code in shortfx/fxString/string_regex.py
regex_extract_all(text: str, pattern: str, case_sensitive: bool = True) -> list[str]
¶
Extracts all matches of a regex pattern from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to search. |
required |
pattern
|
str
|
The regex pattern to find. |
required |
case_sensitive
|
bool
|
If False, performs case-insensitive matching. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of all matching strings. Empty list if no matches found. |
Raises:
| Type | Description |
|---|---|
error
|
If the pattern is not a valid regex. |
Example
regex_extract_all("a1 b2 c3", r"[a-z]\d") ['a1', 'b2', 'c3'] regex_extract_all("no digits here", r"\d+") []
Complexity: O(n*m)
Source code in shortfx/fxString/string_regex.py
regex_match(text: str, pattern: str, case_sensitive: bool = True) -> bool
¶
Checks whether a string matches a regular expression pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to test. |
required |
pattern
|
str
|
The regex pattern to match against. |
required |
case_sensitive
|
bool
|
If False, performs case-insensitive matching. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the pattern matches anywhere in the string. |
Raises:
| Type | Description |
|---|---|
error
|
If the pattern is not a valid regex. |
Example
regex_match("Hello World 123", r"\d+") True regex_match("abc", r"^[A-Z]+$") False
Complexity: O(n*m)
Source code in shortfx/fxString/string_regex.py
regex_split(text: str, pattern: str, max_split: int = 0, case_sensitive: bool = True) -> list[str]
¶
Splits a string by a regex pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input string to split. |
required |
pattern
|
str
|
The regex pattern to use as delimiter. |
required |
max_split
|
int
|
Maximum number of splits (0 = unlimited). |
0
|
case_sensitive
|
bool
|
If False, performs case-insensitive matching. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of substrings. |
Raises:
| Type | Description |
|---|---|
error
|
If the pattern is not a valid regex. |
Example
regex_split("one, two; three four", r"[,;\s]+") ['one', 'two', 'three', 'four']
Complexity: O(n*m)