Regular Expressions (std::regex)
std::regex provides the four fundamental operations on patterns: testing for
presence, finding all occurrences, replacing and splitting. The module accepts
both PCRE syntax (\d, \s) and Lua patterns (%d, %s) — consult the
inline comments of each example to know which dialect applies.
Test
regex.test(pattern, text) returns true if the pattern occurs anywhere in
the text. Use ^ and $ to anchor to the start and end:
Email validation and list filtering with filter + regex.test.
// Feature: regex.test — checks whether the string contains the pattern
// When to use: validate format (email, phone), filter a list by pattern.
use std::regex
use std::Array
// Matches any sequence of digits.
print(regex.test("\\d+", "abc123")) // expected: true
print(regex.test("\\d+", "abcdef")) // expected: false
// Anchored — entire string must be only digits.
print(regex.test("^\\d+$", "12345")) // expected: true
print(regex.test("^\\d+$", "12a45")) // expected: false
// Simplified email (without {n,} quantifier: we use `+`).
let email_re = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]+$"
print(regex.test(email_re, "alice@example.com")) // expected: true
print(regex.test(email_re, "not an email")) // expected: false
// Filter emails out of a list.
let inputs = ["alice@a.com", "ops", "bob@b.io"]
let valid = inputs.filter(|e| regex.test(email_re, e))
print(valid.len()) // expected: 2
Find All
regex.find_all(pattern, text) returns an array of objects with text,
start, stop and groups. regex.count is a shortcut when only the
number matters:
Extracting numbers and words; nums[0].text accesses the substring.
// Feature: regex.find_all — every match of the pattern
// When to use: extract tokens (numbers, IDs, hashtags) from text.
use std::regex
// Each match is a table with `text`, `start`, `stop`, `groups`.
// NOTE: Zolo's regex module uses LUA patterns (`%d`, `%a`, ...),
// not PCRE syntax (`\d`).
let text = "I have 3 apples, 12 pears and 100 grapes"
let nums = regex.find_all("%d+", text)
print(nums.len()) // expected: 3
// The `text` field carries the exact substring.
print(nums[0].text) // expected: 3
print(nums[1].text) // expected: 12
print(nums[2].text) // expected: 100
// Extract words (alphanumeric tokens).
let words = regex.find_all("[a-z]+", "hello world zolo")
print(words.len()) // expected: 3
print(words[0].text) // expected: hello
print(words[2].text) // expected: zolo
// regex.count — shortcut that counts without collecting text.
print(regex.count("%d+", text)) // expected: 3
Replace All
regex.replace_all(pattern, text, replacement) replaces all occurrences. The
replacement can reference capture groups with $1:
Masking digits, normalising spaces and swapping HTML tags for Markdown.
// Feature: regex.replace_all — replace every match
// When to use: anonymize data, strip noise, sanitize input.
use std::regex
// Mask every digit.
print(regex.replace_all("\\d", "id-12345", "*"))
// expected: id-*****
// Collapse multiple spaces into a single one.
print(regex.replace_all(" +", "a b c", " "))
// expected: a b c
// Strip a numeric prefix.
print(regex.replace_all("^\\d+ ?", "42 abc", ""))
// expected: abc
// Swap <b> tags for **.
print(regex.replace_all("<b>(.*?)</b>", "hello <b>world</b>!", "**$1**"))
// expected: hello **world**!
Split
regex.split(pattern, text) splits the string at every position where the
pattern matches — useful for variable separators or multiple delimiters:
Splitting on multiple spaces and on a delimiter set [,;|].
// Feature: regex.split — split a string by pattern
// When to use: split on multiple delimiters, varied whitespace.
use std::csv
use std::regex
// Split on any run of whitespace (1 or more).
let parts = regex.split("\\s+", "one two three four")
print(parts.len()) // expected: 4
print(parts[0]) // expected: one
print(parts[3]) // expected: four
// Split on comma, semicolon, or pipe.
let csv = regex.split("[,;|]", "a,b;c|d,e")
print(csv.len()) // expected: 5
print(csv[0]) // expected: a
print(csv[4]) // expected: e
// When the pattern does not match, returns the whole string in a single-element array.
let single = regex.split(",", "abc")
print(single.len()) // expected: 1
print(single[0]) // expected: abc
Challenge
Use regex.find_all to extract all e-mail addresses from a free-form text
and regex.replace_all to replace them with [REDACTED].
See also