Regex


Quick Start

(module demo
  (import std.regex std.io)
  (begin
    (println (regex:match? "^foo\\d+$" "foo42"))
    (println (regex:replace "(\\d+)" "item-42" "<$1>"))
    (println (regex:find-all "(\\d+)" "a1 b22 c333"))))

std.regex is backed by C++ std::regex with ECMAScript syntax.


API

Compile and Introspection

FunctionSignatureNotes
regex:compile(pattern . flags) -> regexCompile once and reuse.
regex?(x) -> boolRegex object predicate.
regex:pattern(regex) -> stringOriginal source pattern.
regex:flags(regex) -> listSymbol flags (ecmascript, icase, …).

Query and Transform

FunctionSignatureNotes
regex:match?(regex-or-pattern input) -> boolFull-string match.
regex:search(regex-or-pattern input [start]) -> match-or-#fFirst match from start.
regex:find-all(regex-or-pattern input) -> listAll match payloads.
regex:replace(regex-or-pattern input replacement) -> stringSupports $&, $1, $<name>, $$.
regex:replace-fn(regex-or-pattern input fn) -> stringfn receives a match payload and returns replacement text.
regex:split(regex-or-pattern input) -> vectorRegex delimiter split.
regex:quote(s) -> stringEscape regex metacharacters in s.

Match Payload Accessors

FunctionSignatureNotes
regex-match?(x) -> boolMatch payload predicate.
regex-match-start(m) -> intStart offset (inclusive).
regex-match-end(m) -> intEnd offset (exclusive).
regex-match-span(m) -> (start . end)Whole-match span.
regex-match-text(m) -> stringWhole-match substring.
regex-match-group-span(m i) -> (start . end) or #fGroup span by index.
regex-match-group(m i) -> string or #fGroup text by index.
regex-match-named(m sym) -> string or #fNamed group text.

Syntax Notes (ECMAScript)

Common constructs:

Caveats vs PCRE-style engines:


Performance and Safety