Skip to content

Regular expressions ​

Literal and ~ ​

/…/flags is a value of type regex. ~ and !~ answer true/false.

mzs
"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 ​

FlagWhat it does
icase-insensitive (Unicode)
m or s. matches a line break too
xspaces and # comments in the pattern are ignored
mzs
"ЁЖ" ~ /ёж/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:

mzs
"x\nfoo" ~ /^foo$/  # → true
"x\nfoo" ~ /\Afoo/  # → false

Cyrillic ​

\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.

mzs
"Привет" ~ /^\w+$/  # → false
"Привет" ~ /^\p{L}+$/  # → true
"мама мыла раму".matches(/[а-яё]+/)  # → ["мама","мыла","раму"]
"еда, а не еды" ~ /\bеды\b/  # → true

Functions ​

All of them work both as s.f(re) and as re.f(s).

FunctionWhat it returnsExample
capturesthe first match: whole and groups, or nil"a-1".captures(/(\w)-(\d)/) → ["a-1","a","1"]
matchesall matches (groups, if there are any)"a1b2".matches(/\d/) → ["1","2"]
indexposition of the first match in characters, or nil"привет".index(/вет/) → 3
replacereplaces all matches"a1b22".replace(/\d+/, "#") → "a#b#"
replace_firstreplaces the first"a1b22".replace_first(/\d+/, "#") → "a#b22"
splitsplits"a, b;c".split(/[,;]\s*/) → ["a","b","c"]
countnumber of matches"a1b22c333".count(/\d+/) → 3
source, flagspattern text, flags/x/i.flags → "i"

Groups ​

mzs
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/)  # → nil

Replacement ​

In a replacement string \1, \2 are groups (easier in single quotes). A closure gets the match and the groups.

mzs
"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.

mzs
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:

mzs
regex("(")
text
regex: cannot compile

In match ​

mzs
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:

mzs
"1500 ₽".captures(/\d+(?= ₽)/)[0]  # → "1500"
"main.mzs" ~ /^(?!test_).*\.mzs$/  # → true
"the the".captures(/\b(\w+) \1\b/)  # → ["the the","the"]
"Hello" ~ /^[[:alpha:]]+$/  # → true

Complex patterns are bounded by the step budget: catastrophic backtracking gives an error, not a hang.