Regular expressions for Cure (v0.27.0).

Thin wrapper over OTP's :re module. A Regex value is an opaque handle returned by compile/1; every operation takes either a Regex or a raw pattern string (for one-off queries). Patterns supplied as strings are compiled on each call; hot paths should memoise a compile/1 result at the caller's discretion.

The @regex "..." compile-time decorator (recognised by Cure.Stdlib.CompileAssert) hoists pattern validation into the compiler: a malformed pattern surfaces as E060 Regex Invalid at build time rather than at call time.

The runtime module is :cure_std_regex.

Examples

use Std.Regex

let r = compile_bang("h[eo]llo")
is_match(r, "hello")                      # => true
is_match(r, "yellow")                     # => false
use Std.Regex

let digits = compile_bang("[0-9]+")
scan(digits, "ada 1815, lovelace 1852")
# => [Matched{whole: "1815", groups: []}, Matched{whole: "1852", groups: []}]
replace(digits, "ada 1815", "####")      # => "ada ####"

Types

  • type RegexError = InvalidPattern | NotMatched

    Error tag for compile-time and runtime failures.

Functions

  • # fn __group__() -> Atom

    Group tag consumed by Cure.Stdlib.Preload.

  • # fn compile(pattern: String) -> Result(Regex, RegexError) extern

    Compile a pattern string into an opaque Regex handle. Returns Error(InvalidPattern(_)) when the pattern is invalid.

  • # fn compile_bang(pattern: String) -> Regex extern

    Compile and unwrap; raises ArgumentError at runtime if the pattern is invalid. Prefer compile/1 plus explicit Result handling in library code.

  • # fn is_match(r: Regex, input: String) -> Bool extern

    true when the input matches the regex at least once.

  • # fn replace(r: Regex, input: String, replacement: String) -> String extern

    Replace every match with replacement. Backreferences like \1, \2, ... are honoured inside replacement.

  • # fn run(r: Regex, input: String) -> Option(Matched) extern

    Return the first match. None() when the pattern does not match.

  • # fn scan(r: Regex, input: String) -> List(Matched) extern

    Return every match in source order.

  • # fn split(r: Regex, input: String) -> List(String) extern

    Split the input string on every non-overlapping match.