regex Module
The regex module provides powerful regular expression pattern matching, substring search, global match collection, replacements, and capture group extraction.
Importing
djazair
use regex
Convenience Functions
djazair
use regex
# 1. fullMatch — exact match against full string
let isValidEmail = regex.fullMatch("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", "user@example.com")
print("Valid email? ${isValidEmail != Null}") # => True
# 2. search — finds first substring match
let matched = regex.search("\d+", "Order #12894 approved")
if matched != Null
print("Found order ID: ${matched.group(0)}") # => 12894
end
# 3. findAll — collects all occurrences
let words = regex.findAll("[A-Z][a-z]+", "Algiers Oran Constantine Annaba")
print("Cities: ${words}") # => ["Algiers", "Oran", "Constantine", "Annaba"]
# 4. sub — replaces matched patterns
let cleaned = regex.sub("\s+", " ", "Too many spaces")
print(cleaned) # => "Too many spaces"
Compiled Pattern Objects
For high performance inside loops, pre-compile patterns using regex.compile(pattern, flags?):
djazair
use regex
let phonePattern = regex.compile("(\+\d{1,3})\s+(\d{9})")
let m = phonePattern.search("Contact: +213 555123456")
if m != Null
print("Country code: ${m.group(1)}") # => +213
print("Local number: ${m.group(2)}") # => 555123456
print("Match span: ${m.start()} to ${m.endPos()}")
end