docs / Getting Started / system.text

system.text

system.text provides regular expressions. Regex.compile returns a Result<Regex, RegexError> rather than panicking, so a malformed pattern is a value you handle — the same discipline as every other fallible operation in the standard library.

use system.io
use system.text

fn main()
    // Regex.compile reports a bad pattern as a value, never a panic.
    match Regex.compile("[0-9]+")
        Result.Ok(digits)
            let has_digit = digits.matches("a1b2")
            println(f"contains a digit: {has_digit}")

            // find returns the first Match, or None.
            match digits.find("port 8080, fallback 9090")
                Some(m): println(f"{m.text()} at {m.start()}..{m.end()}")
                None: println("no digits")

            // find_all returns every non-overlapping match.
            for m in digits.find_all("a1b2c3")
                println(f"found {m.text()}")

            println(digits.replace("a1b2c3", "#"))
        Result.Err(e): println(f"bad pattern: {e}")
MemberReturnsBehaviour
Regex.compile(pattern String)Result<Regex, RegexError>Static. Compiles a pattern, reporting a syntax error as Err.
matches(text String)boolWhether the pattern occurs anywhere in text.
find(text String)Match?The first match, or None.
find_all(text String)[Match]Every non-overlapping match, in order.
replace(text String, to String)StringEvery match replaced with to.
Match.text() / start() / end()String / int / intThe matched text and its half-open byte range.

A pattern that is fixed at compile time can be written as a regex literal — re"^\d+$", with optional trailing flags — which the compiler validates while it type-checks, turning an invalid pattern into a compile error instead of a runtime Err. String also gained split, join, to_int and to_float, and match arms accept string, float and regex predicates.