Overview of the Regex Module
The Regex module provides comprehensive regular expression operation capabilities, supporting pattern matching, searching, capturing group extraction, text splitting, and replacement operations. All functions are implemented based on the regex-lite
library, providing efficient regular expression processing capabilities.
Function Overview
Function Category | Main Functions | Purpose |
---|---|---|
Matching and Locating | find , find_all |
Find match positions and content |
Matching Validation | match |
Validate if the entire text matches the pattern |
Capturing Group Operations | capture , captures , capture_name |
Extract captured group content |
Text Processing | split , replace |
Text splitting and replacement |
Matching and Locating Functions
find <pattern> <text>
- Find the first match
- Parameters:
pattern
(required):String|Regex
- Regular expression patterntext
(required):String
- Text to search
- Returns:
Map|None
- Match information mapping, containingstart
,end
, andfound
fields; returns None if not found - Example:
Regex.find(r'\d+', "abc123def")
returns{start: 3, end: 6, found: "123"}
find_all <pattern> <text>
- Find all matches
- Parameters:
pattern
(required):String|Regex
- Regular expression patterntext
(required):String
- Text to search
- Returns:
List[Map]
- List of all matches, each element containsstart
,end
, andfound
fields - Example:
Regex.find_all(r'\d+', "abc123def456")
returns a list of all matched numbers
Matching Validation Functions
match <pattern> <text>
- Check if the entire text matches the pattern
- Parameters:
pattern
(required):String|Regex
- Regular expression patterntext
(required):String
- Text to validate
- Returns:
Boolean
- Returns true if the entire text matches
Capturing Group Operation Functions
capture <pattern> <text>
- Get the first matching capturing group
- Parameters:
pattern
(required):String|Regex
- Regular expression containing capturing groupstext
(required):String
- Text to match
- Returns:
List|None
- List of captured groups, index 0 is the full match, subsequent indices are each capturing group - Example:
Regex.capture(r'(\d{4})-(\d{2})-(\d{2})', "2023-12-25")
returns["2023-12-25", "2023", "12", "25"]
captures <pattern> <text>
- Get all matching capturing groups
- Parameters:
pattern
(required):String|Regex
- Regular expression containing capturing groupstext
(required):String
- Text to match
- Returns:
List[List]
- List of all matching capturing groups
capture_name <pattern> <text>
- Get named capturing groups
- Parameters:
pattern
(required):String|Regex
- Regular expression containing named capturing groupstext
(required):String
- Text to match
- Returns:
Map|None
- Mapping of named capturing groups, with keys as group names and values as matched content - Example:
Regex.capture_name(r'(?P<year>\d{4})-(?P<month>\d{2})', "2023-12")
returns{year: "2023", month: "12"}
Text Processing Functions
split <pattern> <text>
- Split text by regular expression
- Parameters:
pattern
(required):String|Regex
- Splitting patterntext
(required):String
- Text to split
- Returns:
List[String]
- List of split strings - Example:
Regex.split(r'\s+', "hello world test")
returns["hello", "world", "test"]
replace <pattern> <replacement> <text>
- Replace all matches
- Parameters:
pattern
(required):String|Regex
- Matching patternreplacement
(required):String
- Replacement contenttext
(required):String
- Target text
- Returns:
String
- Text after replacement - Example:
Regex.replace(r'\d+', "X", "abc123def456")
returns"abcXdefX"
Parameter Handling Mechanism
The functions in the Regex module support flexible parameter type handling. They can process different combinations of parameters:
- Support for both
Regex
type andString
type pattern parameters - Automatically compiles strings into regular expressions
- Provides detailed error messages
Usage Examples
Basic Matching Operations
# Find numbers |
Capturing Group Operations
# Parse date |
Text Processing
# Split text |
Pipeline Operation Examples
# Extract all numbers and sum them |
Notes
The Regex module is based on the regex-lite
library, providing efficient regular expression processing capabilities. All functions support both string and Regex
type pattern parameters, automatically handling type conversions. The capturing group functionality is particularly powerful, supporting both positional and named captures. Parameter type descriptions indicate that <>
denotes required parameters and []
denotes optional parameters.
It is recommended to use raw strings (e.g., r'pattern'
) to avoid the complexity of escape characters.
In practical use, chained calls are supported, such as:
let rg = r'[,;]\s*' |
In the examples, type names are used for clarity.