Regex
Regex holds the functions of regular expressions, written as a literal /.../flags or compiled from data with Regex.compile. The syntax is RE2, so a match takes time linear in the input; indices into the searched string count code points.
Details
- Write a pattern fixed in the source as a literal, such as
/\d+/i;ant checkrejects a bad one. Compile a pattern from data with Regex.compile, which returns aResult. - RE2 has no lookahead, lookbehind, or backreferences.
\d,\w,\s, and\bmatch ASCII only. Use\pL,\pN, or\p{Greek}for Unicode classes.- A pattern is immutable and compares by its source and flags, so it can be a map key.
Basic examples
Regex.test asks whether a pattern matches anywhere in a string:
Regex.test(/\d+/, "room 42") // true
Regex.test(/^\d+$/, "42a") // falseRegex.find returns the leftmost match, with its groups, or null:
let email = /(?<user>\w+)@(?<host>[\w.]+)/
Regex.find(email, "to ann@example.com")?.named // Map{ "user": "ann", "host": "example.com" }
Regex.find(email, "no address") // nullRegex.replace replaces every match, and $n names a group:
Regex.replace(/\s+/, "a b \t c", " ") // "a b c"
Regex.replace(/(\w+)@(\w+)/, "ann@home bob@work", "$2:$1") // "home:ann work:bob"Regex.compile builds a pattern from a string and returns a Result:
Regex.compile("a+", "i") // Ok{ value: /a+/i }
Regex.compile("[a-z") // Err{ error: Error{ message: "invalid regular expression: missing closing ]: `[a-z`" } }Scope
Flags
The flags i, m, and s follow the closing slash, in any order. i ignores case, m makes ^ and $ match at line ends, and s lets . match a line break:
Regex.test(/ant/i, "ANT") // true
Regex.test(/^b/, "a\nb") // false
Regex.test(/^b/m, "a\nb") // true
Regex.test(/a.b/, "a\nb") // false
Regex.test(/a.b/s, "a\nb") // true
Regex.flags(/ant/si) // "is"Groups
groups[0] is the whole match and groups[n] the nth group, null when the group took no part. named holds the named groups:
let m = Regex.find(/(\d+)(px|em)?/, "width 12")
m?.groups // ["12", "12", null]Unicode
The ASCII classes skip letters and digits outside ASCII. The Unicode classes include them, and the indices count code points:
Regex.findAll(/\w+/, "héllo") | Array.map({ x.text }) // ["h", "llo"]
Regex.findAll(/\pL+/, "héllo") | Array.map({ x.text }) // ["héllo"]
Regex.test(/\d/, "٣") // false
Regex.test(/\pN/, "٣") // true
Regex.find(/b/, "→b")?.start // 1Dot calls and pipes
A pattern takes the dot form, since Regex is its namespace. The pattern is the first argument, so a pipe carries a pattern into a Regex function, not the string it searches; pipe the results onward instead:
let word = /[a-z]+/
word.test("42 apples") // true
word.findAll("one two three") | Array.map({ x.text }) // ["one", "two", "three"]Patterns as values
A pattern compares by value and serves as a map key:
let kinds = Map{ /^\d+$/: "number", /^[a-z]+$/: "word" }
let digit = /[0-9]/
kinds[/^\d+$/] // "number"
digit == /[0-9]/ // true
digit == /\d/ // falseis Regex.Pattern tells a pattern from the other members of a union:
func describe(rule: Regex.Pattern | string): string {
if rule is Regex.Pattern { "pattern ${rule}" } else { "text ${rule}" }
}
println(describe(/^a+$/i))
println(describe("abc"))pattern /^a+$/i
text abcFunctions by job
Building a pattern
- Regex.compile: a pattern from a source string and flags, or an
Err - Regex.escape: a string with every metacharacter escaped, to match it literally
Matching
- Regex.test: whether the pattern matches anywhere
- Regex.find: the leftmost match, or
null - Regex.findAll: every match, left to right, without overlap
Replacing
- Regex.replace: every match replaced, with
$nand${name}expanded - Regex.replaceFirst: the first match replaced, expanded the same way
- Regex.replaceWith: every match replaced by what a function returns for it
Splitting
- Regex.split: the pieces of a string between the matches
Reading a pattern
- Regex.source: the normalized source
- Regex.flags: the flags, sorted
Types
- Regex.Pattern: a compiled regular expression
- Regex.Match: one match, with its text, indices, and groups
Applications
Parse log lines into records with named groups:
type LogLine = { level: string, message: string }
let line = /^\[(?<level>[A-Z]+)\] (?<message>.*)$/
func parse(text: string): LogLine | null {
let m = line.find(text)
if m == null {
return null
}
LogLine{ level: m.named["level"] ?? "", message: m.named["message"] ?? "" }
}
println(parse("[WARN] disk almost full"))
println(parse("garbage"))LogLine{ level: "WARN", message: "disk almost full" }
nullValidate a whole input by anchoring the pattern with ^ and $:
let isoDate = /^\d{4}-\d{2}-\d{2}$/
["2026-11-12", "12/11/2026", "2026-11-12x"].map({ isoDate.test(x) }) // [true, false, false]Mask every number but its last two digits:
Regex.replaceWith(/\d+/, "card 4111111111111111, pin 1234", {
let n = x.text.length()
"*".repeat(n - 2) + x.text.slice(n - 2, n)
}) // "card **************11, pin **34"Split a list whose separators vary:
Regex.split(/\s*[,;]\s*/, "red, green ;blue, gray") // ["red", "green", "blue", "gray"]Turn a title into a URL slug: replace each run of other characters with a dash, then drop the dashes at the ends:
let title = " Hello, World: Ant 1.0! "
let dashed = Regex.replace(/[^a-z0-9]+/, title.toLower(), "-")
Regex.replace(/^-|-$/, dashed, "") // "hello-world-ant-1-0"Properties and relations
Regex.find gives null exactly when Regex.test is false, and a match's indices slice out its text:
let p = /o+/
for s in ["foo", "bar", "bόo"] {
let m = p.find(s)
assert((m != null) == p.test(s))
if m != null {
assert(s.slice(m.start, m.end) == m.text)
}
}A pattern compiled from Regex.escape(s) matches s itself:
for s in ["a.b", "1+1=2", "(x)"] {
let p = s | Regex.escape() | Regex.compile() | Result.unwrap()
assert(p.test(s))
assert(Regex.replace(p, s, "") == "")
}A literal and Regex.compile with the same source and flags give the same pattern:
assert(Regex.compile(`\d+`, "i") == Ok{ value: /\d+/i })Possible issues
Regex is a namespace, not a type. Annotate with Regex.Pattern:
func hasDigit(p: Regex, s: string): bool { p.test(s) } // error: Regex is a namespace, not a typeThere is no g flag. Regex.findAll, Regex.replace, and Regex.split already work on every match:
let all = /a/g // error: there is no g flag: Regex.findAll and Regex.replace work on every matchRE2 has no lookahead, lookbehind, or backreferences, and a literal with one does not compile:
let p = /\d+(?=px)/
// error: lookahead is not supported: a pattern is RE2, which has no lookahead, lookbehind or backreferencesA line that starts with a literal continues the line before it when that line ends in an operand, such as ), and the / reads as division. Start the line with Regex.test(...) or name the pattern with let:
let s = "a\nb"
println(s.length())
/^b/m.test(s) // error: unexpected character '^'Regex.compile returns a Result, so handle the Err before matching:
let r = Regex.compile("a+")
let ok = r.test("aa") // error: Result[Regex.Pattern, Error] has no field or function testUnwrap it with try in a function that returns a Result:
func matches(source: string, s: string): Result[bool, Error] {
let p = try Regex.compile(source)
Ok{ value: p.test(s) }
}
println(matches("a+", "caat"))
println(matches("[a-z", "caat"))Ok{ value: true }
Err{ error: Error{ message: "invalid regular expression: missing closing ]: `[a-z`" } }// begins a comment, so the empty pattern has no literal. Write Regex.compile(""):
Regex.compile("") // Ok{ value: /(?:)/ }In a string, ${1} interpolates the number 1 instead of naming group 1. Use a raw string or \${:
Regex.replace(/(\w+)/, "ann", "${1}x") // "1x"
Regex.replace(/(\w+)/, "ann", `${1}x`) // "annx"$1x names a group 1x, which does not exist, so it expands to an empty string. Write ${1}x in a raw string to put text after a group:
Regex.replace(/(\w+)/, "ann", "$1x") // ""i folds case one code point at a time, so it does not match ß against SS. Compare whole strings with String.equalFold instead:
Regex.test(/straße/i, "STRASSE") // false
"straße".equalFold("STRASSE") // trueSee also
- Regular expression literals: the
/.../flagssyntax - String:
String.contains,String.replace, andString.splitfor fixed text, with no pattern - Text guide: working with strings across namespaces
