Regex

Regex is a first-class type with literal syntax and a Thompson NFA engine.

Summary

Regex literals use forward slashes: /[0-9]+/. The engine is a Thompson NFA supporting POSIX extended syntax plus \d, \w, \s shortcuts, non-greedy quantifiers (*?, +?), non-capturing groups ((?:...)), and positive and negative lookaheads. Methods on a regex value: .test(), .match(), .replace(), and .source(). Regex literals also work directly as match patterns.

Canonical

Regex is a first-class type (re) with literal syntax using forward slashes.

let pat = /[0-9]+/
println(type(pat))               -- re

-- with type annotation
let digits: re = /[0-9]+/

The engine is a Thompson NFA. Beyond POSIX extended syntax it recognises the common shortcuts \d, \D, \s, \S, \w, \W, \b; non-greedy quantifiers *? / +? / ??; non-capturing groups (?:...); and positive (?=...) / negative (?!...) lookaheads.

Regex Methods

let pat = /[0-9]+/

pat.test("abc123")               -- true (matches anywhere in string)
pat.match("abc123")              -- "123" (first match, or null)
pat.replace("abc123def", "NUM")  -- "abcNUMdef"
pat.source()                     -- "[0-9]+"

Regex in Pattern Matching

Regex literals work as match patterns, testing the value against the pattern:

fn classify(s) {
    match s {
        /^[0-9]+$/ => "number"
        /^[a-z]+$/ => "lowercase"
        /^[A-Z]+$/ => "uppercase"
        _ => "mixed"
    }
}