regex
Regular expressions: test whether text matches a pattern, find matches and their groups, replace and split.
regex.compile turns a pattern into a Regex, which can be used any number of times. Matching runs as a Pike VM, so the time it takes grows linearly with the text, whatever the pattern: no pattern can make a match run for hours. The price is that there are no backreferences (\1) and no lookaround.
Write patterns in normal string literals, so a backslash is doubled: "\\d+" is the pattern \d+.
Test and find
Section titled “Test and find”import regex
let date = try regex.compile("(\\d{4})-(\\d{2})-(\\d{2})")print(date.is_match("released 2026-11-26"))let m = date.find("released 2026-11-26, patched 2026-12-01").unwrap()print(f"{m.text} at {m.start}..{m.end}")print(m.groups[1].clone().unwrap())for d in date.find_all("2026-11-26 and 2026-12-01") { print(d.text)}true2026-11-26 at 9..1920262026-11-262026-12-01Replace and split
Section titled “Replace and split”import regex
let date = try regex.compile("(\\d{4})-(\\d{2})-(\\d{2})")print(date.replace_all("from 2026-11-26 to 2026-12-01", "$3.$2.$1"))let comma = try regex.compile("\\s*,\\s*")print(comma.split("a , b,c ,d").join("|"))from 26.11.2026 to 01.12.2026a|b|c|dHandle a bad pattern
Section titled “Handle a bad pattern”RegexError gives the byte position in the pattern and a message.
import regex
for pattern in ["a(b", "a{2,1}", "[a-z]+"] { let r = regex.compile(&pattern) catch { regex.RegexError(e) => { print(f"{pattern}: at {e.position}: {e.message}") continue } } print(f"{pattern}: ok, {r.group_count()} group(s)")}a(b: at 3: missing `)`a{2,1}: at 6: in `{m,n}`, n must not be less than m[a-z]+: ok, 0 group(s)Reference
Section titled “Reference”import regexFunctions
Section titled “Functions”compile
Section titled “compile”pub fn compile(pattern: &Str) -> Regex ! RegexError(?i) at the very start makes letters match in either case.
Supported syntax:
| Pattern | Meaning |
|---|---|
. |
any character except a line break |
[abc], [a-z], [^x] |
one character in, or not in, the set |
\d \w \s |
digit, word character ([A-Za-z0-9_]), white space; \D \W \S are the opposites |
^ $ |
start and end of the text |
\b \B |
word boundary, not a word boundary |
( ) |
a group that captures |
(?: ) |
a group that does not capture |
a|b |
either |
* + ? {m} {m,} {m,n} |
repeats, greedy |
*? +? ?? {m,n}? |
repeats, lazy (as few as possible) |
\d, \w, \s and \b follow Unicode: \w+ finds héllo and Россия whole, and \d matches Arabic-Indic digits too (use [0-9] for ASCII digits only). Start the pattern with (?i) to match letters in either case: (?i)été matches ÉTÉ.
pub struct Regex {}A regular expression compiled to a small program and run as a Pike VM: time grows linearly.
It also has 4 private field(s), so outside this module it is made only by this module’s functions, which check what goes in.
Regex.is_match
Section titled “Regex.is_match”pub fn is_match(&self, text: &Str) -> boolRegex.find
Section titled “Regex.find”pub fn find(&self, text: &Str) -> Option<Match>Regex.find_at
Section titled “Regex.find_at”pub fn find_at(&self, text: &Str, start: i64) -> Option<Match>The first match starting at or after byte start.
start is a byte offset. Patterns starting with ^ match only at the very start of the text, not at start.
Regex.find_all
Section titled “Regex.find_all”pub fn find_all(&self, text: &Str) -> Vec<Match>Every match, left to right, not overlapping; after an empty match the search moves on one character.
Regex.replace_all
Section titled “Regex.replace_all”pub fn replace_all(&self, text: &Str, with: &Str) -> StrReplaces every match; $0..$9 in with insert the match and its groups.
In the replacement, $0 is the whole match, $1 to $9 are groups, ${10} names any group by number, and $$ is a dollar sign. A group that took no part inserts nothing. $10 is group 1 followed by 0; write ${10} for group ten. replace changes only the first match.
Regex.replace
Section titled “Regex.replace”pub fn replace(&self, text: &Str, with: &Str) -> StrReplaces only the first match, with $n and ${n} filled in as in replace_all.
Regex.split
Section titled “Regex.split”pub fn split(&self, text: &Str) -> Vec<Str>The pieces of text between matches.
A pattern that can match empty text splits between every character, but not at the very start or end:
import regex
let any = try regex.compile("x*")print(any.split("abc").join("|"))print(any.replace_all("abc", "-"))a|b|c-a-b-c-Regex.group_count
Section titled “Regex.group_count”pub fn group_count(&self) -> i64How many (...) groups the pattern has.
RegexError
Section titled “RegexError”pub struct RegexError { pub message: Str pub position: i64}pub struct Match { pub start: i64 pub end: i64 pub text: Str pub groups: Vec<Option<Str>>}groups[0] is the whole match; groups[k] is the k-th (...), None if it took no part.
groups[0] is the whole match. groups[k] is the k-th (...) group, counted by its opening parenthesis, and is None when that group took no part in the match (for example (b)? when there was no b). Positions are byte offsets into the text.