Regular expressions
Literal and ~
/…/flags is a value of type regex. ~ and !~ answer true/false.
"Hello!" ~ /hello/i # → true
"hello" !~ /\d/ # → true
nil ~ /a/ # → false== with a regular expression is a compile error; matching is only through ~. The other side of ~ is a string or nil, otherwise a type error.
Flags
| Flag | What it does |
|---|---|
i | case-insensitive (Unicode) |
m or s | . matches a line break too |
x | spaces and # comments in the pattern are ignored |
"ЁЖ" ~ /ёж/i # → true
"a\nb" ~ /a.b/ # → false
"a\nb" ~ /a.b/m # → true
"A-1043" ~ / [A-Z] - \d+ /x # → true^ and $ are always the start and end of a line. The start and end of the whole text are \A and \z:
"x\nfoo" ~ /^foo$/ # → true
"x\nfoo" ~ /\Afoo/ # → falseCyrillic
\w, \d, \s are ASCII only. For letters of any alphabet use \p{L} or an explicit class. \b and the i flag understand Unicode.
"Привет" ~ /^\w+$/ # → false
"Привет" ~ /^\p{L}+$/ # → true
"мама мыла раму".matches(/[а-яё]+/) # → ["мама","мыла","раму"]
"еда, а не еды" ~ /\bеды\b/ # → trueFunctions
All of them work both as s.f(re) and as re.f(s).
| Function | What it returns | Example |
|---|---|---|
captures | the first match: whole and groups, or nil | "a-1".captures(/(\w)-(\d)/) → ["a-1","a","1"] |
matches | all matches (groups, if there are any) | "a1b2".matches(/\d/) → ["1","2"] |
index | position of the first match in characters, or nil | "привет".index(/вет/) → 3 |
replace | replaces all matches | "a1b22".replace(/\d+/, "#") → "a#b#" |
replace_first | replaces the first | "a1b22".replace_first(/\d+/, "#") → "a#b22" |
split | splits | "a, b;c".split(/[,;]\s*/) → ["a","b","c"] |
count | number of matches | "a1b22c333".count(/\d+/) → 3 |
source, flags | pattern text, flags | /x/i.flags → "i" |
Groups
m = "order A-1043".captures(/([A-Z])-(\d+)/)
m # → ["A-1043","A","1043"]
m[2].int # → 1043
d = "2026-08-13".captures(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/)
d["y"] # → "2026"
d[3] # → "13"
"a=1, b=2".matches(/(\w+)=(\d+)/) # → [["a","1"],["b","2"]]
"abc".captures(/\d/) # → nilReplacement
In a replacement string \1, \2 are groups (easier in single quotes). A closure gets the match and the groups.
"Ivan Petrov".replace(/(\p{L}+) (\p{L}+)/, '\2 \1') # → "Petrov Ivan"
"abc".replace(/b/) { it.upper } # → "aBc"
"price: 100, 250".replace(/\d+/) { (it.int * 2).str } # → "price: 200, 500"Pattern from a string
regex(string, flags) builds an expression at run time. In "…" the backslash has to be doubled, in '…' it does not.
word = "peace"
re = regex('\b' + word + '\b', "i")
"Peace and work" ~ re # → true
regex("^\\d+$").source # → "^\\d+$"
regex('^\d+$').source # → "^\\d+$"A bad pattern is an error of kind regex:
regex("(")regex: cannot compileIn match
fn intent(text) {
match text {
/^(hello|hi)/i -> "greet"
/operator|human/i -> "handoff"
else -> "other"
}
}
intent("Hello there!") # → "greet"
intent("Get me an operator") # → "handoff"What else is supported
Lookahead and lookbehind, backreferences, atomic groups, POSIX classes:
"1500 ₽".captures(/\d+(?= ₽)/)[0] # → "1500"
"main.mzs" ~ /^(?!test_).*\.mzs$/ # → true
"the the".captures(/\b(\w+) \1\b/) # → ["the the","the"]
"Hello" ~ /^[[:alpha:]]+$/ # → trueComplex patterns are bounded by the step budget: catastrophic backtracking gives an error, not a hang.