Regular expression literals
A pattern between slashes, with optional flags after the closing slash, is a regular expression literal: /^\d+$/ and /ant/i are Regex.Pattern values, as in JavaScript.
Details
- The pattern is RE2 syntax.
ant checkandant runreject an invalid pattern before the program runs, so an invalid literal never reaches run time. UseRegex.compilefor a pattern from data; it returns aResult. - A backslash belongs to the pattern:
\dneeds no doubling.\/is a/, and so is a/inside a character class[...]; neither ends the literal. - The flags are
i(ignore case),m(^and$match at line ends), ands(.matches a line break), each at most once, in any order. - A literal stays on one line. It cannot be empty or start with
*, since//and/*begin comments; write those withRegex.compile. - A pattern is immutable and compared by its source and flags, so two equal literals are
==and a pattern can be a map key.
Basic examples
A literal is a pattern you pass to the Regex functions:
Regex.test(/^\d{3}-\d{4}$/, "555-1234") // true
Regex.test(/^ant$/i, "ANT") // true
Regex.test(/^ant$/, "ANT") // falseA pattern renders as the literal that reads back to it:
[/colou?r/, /a\/b/i] // [/colou?r/, /a\/b/i]Scope
Flags
i ignores case, m lets ^ and $ match at each line, and s lets . match a line break:
let text = "first\nsecond"
Regex.test(/^SECOND/i, "second") // true
Regex.test(/^second/, text) // false
Regex.test(/^second/m, text) // true
Regex.test(/first.second/, text) // false
Regex.test(/first.second/s, text) // trueSlashes and backslashes
\/ and a / inside [...] both match a slash. Escapes such as \d and \. go into the pattern as written:
Regex.test(/^\/usr\//, "/usr/bin") // true
Regex.test(/[/]/, "a/b") // true
Regex.test(/\.ant$/, "main.ant") // trueA backslash in the input is matched with \\, and a raw string holds the input without escapes:
/^C:\\temp$/.test(`C:\temp`) // trueGroups
Groups, named groups, and flag groups such as (?i) work inside a literal:
let date = /(?<year>\d{4})-(?<month>\d\d)/.find("due 2026-11-11")
date?.named["year"] // "2026"
date?.groups[2] // "11"
Regex.test(/(?i)ant/, "ANT") // trueUnicode
\d, \w, \s, and \b match ASCII only. \pL matches a letter in any script, and \p{Greek} a script by name:
let text = "héllo wörld"
Regex.findAll(/\w+/, text).map({ x.text }) // ["h", "llo", "w", "rld"]
Regex.findAll(/\pL+/, text).map({ x.text }) // ["héllo", "wörld"]
Regex.test(/\bllo/, "héllo") // true
Regex.findAll(/\p{Greek}+/, "alpha αβγ").map({ x.text }) // ["αβγ"]é is not a word character to \b, so a boundary falls inside héllo.
Where a literal can start
/ begins a literal wherever no operand comes before it: after (, ,, =, return, or {, and at the start of ${...}:
func isScript(name: string): bool {
return /\.ant$/.test(name)
}
let names = ["main.ant", "notes.txt"]
println(names.filter({ /\.txt$/.test(x) }))
println(isScript("main.ant"))
println("pattern ${/a\/b/}")["notes.txt"]
true
pattern /a\/b/After an operand, such as a name or ), / is division:
let total = 10
let count = 2
total / count // 5Patterns as values
A pattern is a value like any other. Annotate it as Regex.Pattern, keep it in a collection, or use it as a map key:
let rules: Map[Regex.Pattern, string] = Map{ /^\d+$/: "number", /^\w+$/: "word" }
rules[/^\d+$/] // "number"Applications
Validate input
func checkUsername(name: string): Result[string, Error] {
if !/^[a-z][a-z0-9_]{2,15}$/.test(name) {
return Result.fail("username ${name}: use 3 to 16 lowercase letters, digits, or _")
}
Result.ok(name)
}
println(checkUsername("ant_lang"))
println(checkUsername("9lives"))Ok{ value: "ant_lang" }
Err{ error: Error{ message: "username 9lives: use 3 to 16 lowercase letters, digits, or _" } }Rewrite with groups
$1 and $2 in a replacement refer to the groups:
/(\w+)@(\w+)/.replace("ann@home bob@work", "$2:$1") // "home:ann work:bob"Split on a pattern
Regex.split(/\s*,\s*/, "red , green,blue") // ["red", "green", "blue"]
Regex.split(/\d+/, "a1b22c") // ["a", "b", "c"]Pull out every match
let text = "Call 555-1234 or 555-9876"
Regex.findAll(/\d{3}-\d{4}/, text).map({ x.text }) // ["555-1234", "555-9876"]Properties and relations
A literal and Regex.compile with the same source and flags build equal patterns. Flags compare in any order, and \/ in a literal is a plain / in the source:
assert(Regex.compile("a+", "i") == Ok{ value: /a+/i })
assert(/a/si == /a/is)
assert(Regex.compile("a/b") == Ok{ value: /a\/b/ })
assert(/a\/b/i.source() == "a/b")
assert(/a/si.flags() == "is")Possible issues
An invalid pattern is a compile error
let open = /(/ // error: invalid regular expression: missing closing ): `(`The same pattern from data is an Err, not a compile error:
Regex.compile("(") // Err{ error: Error{ message: "invalid regular expression: missing closing ): `(`" } }No lookahead or backreferences
RE2 has no lookahead, lookbehind, or backreferences, so a match takes time linear in the input:
let price = /\d+(?=€)/
// error: lookahead is not supported: a pattern is RE2, which has no lookahead, lookbehind or backreferencesNo g, y, u, v, or d flags
Ant has no g flag. Regex.findAll and Regex.replace work on every match:
let vowels = /[aeiou]/g // error: there is no g flag: Regex.findAll and Regex.replace work on every match/[aeiou]/.replace("banana", "_") // "b_n_n_"JavaScript's other flags are errors too: for y, anchor with ^ on a String.slice of the input; u, v, and d are how Ant already behaves, so drop them:
let word = /\w+/u // error: there is no u flag: a pattern is Unicode and a match carries its indices alreadyA line that starts with a literal
/ after an operand is division, even when a line break comes between them. A line that starts with a literal, right after a line that ends in a name, a value, ), ], or }, is read as a division with no left operand:
let word = "ant"
println(word)
/a/.test(word) // error: unexpected '/'Name the pattern with let, wrap the literal in parentheses, or write the call in namespace form:
let word = "ant"
let a = /a/
println(a.test(word))
(/n/).test(word) | println()
println(Regex.test(/t/, word))true
true
trueAn empty pattern
// begins a comment, so an empty pattern is written with Regex.compile:
Regex.compile("") // Ok{ value: /(?:)/ }Ignoring case is not case folding
i compares one code point at a time, so ß does not match SS. Compare such text with String.equalFold:
Regex.test(/straße/i, "STRASSE") // false
"straße".equalFold("STRASSE") // trueSee also
Regex · Regex.Pattern · Regex.compile · Regex.test · Regex.find · Regex.replace · Raw strings · String literals · Arithmetic operators
