Regular Expressions
Pattern matching with the regex module
Regular Expressions
The regex module provides pattern matching using POSIX extended regular expressions.
Importing
import regexFunctions
match
Check if a pattern matches the entire string.
Syntax: call regex.match with string, pattern
Returns: true if the pattern matches the entire string, false otherwise
Example:
import regex
set matches to call regex.match with "hello", "h.*o" # Returns true
set no_match to call regex.match with "world", "h.*o" # Returns falsesearch
Find the first match of a pattern in a string.
Syntax: call regex.search with string, pattern
Returns: The first matched substring, or null if no match is found
Example:
import regex
set result to call regex.search with "hello world", "world" # Returns "world"
set result2 to call regex.search with "hello world", "xyz" # Returns nullfindall
Find all matches of a pattern in a string.
Syntax: call regex.findall with string, pattern
Returns: A list of all matched substrings (empty list if no matches)
Example:
import regex
set matches to call regex.findall with "cat, bat, sat", "[a-z]at"
# Returns ["cat", "bat", "sat"]
set numbers to call regex.findall with "abc123def456ghi789", "[0-9]+"
# Returns ["123", "456", "789"]Pattern Syntax
The regex module uses POSIX Extended Regular Expression syntax. Common patterns:
.- Matches any character*- Zero or more of the preceding element+- One or more of the preceding element?- Zero or one of the preceding element[abc]- Matches any character in the brackets[a-z]- Matches any character in the range^- Start of string$- End of string\d- Digit (in some regex flavors, but POSIX uses[0-9])
Examples
Email Validation
import regex
function is_valid_email with email:
set pattern to "^[a-z]+@[a-z]+\\.[a-z]+$"
return call regex.match with email, pattern
set email1 to "user@example.com"
set email2 to "invalid-email"
print call is_valid_email with email1 # Prints: true
print call is_valid_email with email2 # Prints: falseExtracting Patterns
import regex
set text to "Contact: alice@example.com or bob@test.org"
set pattern to "[a-z]+@[a-z]+\\.[a-z]+"
set emails to call regex.findall with text, pattern
print emails # Prints: ["alice@example.com", "bob@test.org"]Phone Number Validation
import regex
function validate_phone with phone:
set pattern to "^[0-9]{3}-[0-9]{3}-[0-9]{4}$"
return call regex.match with phone, pattern
set phone1 to "123-456-7890"
set phone2 to "invalid"
print call validate_phone with phone1 # Prints: true
print call validate_phone with phone2 # Prints: falseFinding Words
import regex
set text to "The cat sat on the mat"
set pattern to "[a-z]at"
set matches to call regex.findall with text, pattern
print matches # Prints: ["cat", "sat", "mat"]Error Handling
Invalid regex patterns will raise a RuntimeError:
import regex
try:
set result to call regex.match with "test", "[invalid"
print result
catch error:
print f"Invalid regex pattern: {error}"