Module std::text::regex

Regular expression matching.

Compile patterns once and match against multiple strings.

Examples

import std::text::regex;

fn main() {
    let re = regex.new("[0-9]+");
    if re.is_match("abc123") {
        let m = re.find("abc123");
        println(m);  // "123"
    }
    re.free();
}

Contents

Functions

Function new

pub fn new(pattern: string) -> Pattern

Compile a regular expression pattern.

Panics if the pattern syntax is invalid.

Examples

let email_re = regex.new("[a-z]+@[a-z]+\\.[a-z]+");
email_re.free();

Types

Struct Pattern

A compiled regular expression pattern.

Created by regex.new(pattern). Call free() before the value goes out of scope; dropping without free() is a resource leak.

Struct CaptureMatches

Flat row-major capture results for captures and find_all_submatch.

Each match row has group_count entries. Group 0 is the whole match and groups 1..n are capture groups. Missing optional groups are stored as "".

Fields

groups: Vec<string>
group_count: i64

Traits

Trait PatternMethods

Methods available on a compiled Pattern.

Methods

fn is_match(self: Self, input: string) -> bool

Test whether the pattern matches anywhere in the input string.

Examples
let re = regex.new("^[a-z]+$");
assert(re.is_match("hello"));
re.free();
fn matches(self: Self, input: string) -> bool

Ergonomic alias for is_match. Returns true when the pattern matches anywhere in input. Provided so call sites can read as pattern.matches(text) โ€” a one-to-one replacement for the removed text =~ pattern operator.

fn find(self: Self, input: string) -> string

Find the first match of the pattern in the input string.

Returns the matched substring, or an empty string if no match.

fn replace(self: Self, input: string, replacement: string) -> string

Replace all occurrences of the pattern with the replacement string.

Examples
let re = regex.new("[0-9]+");
let result = re.replace("abc123def456", "N");
// result == "abcNdefN"
re.free();
fn find_all(self: Self, input: string) -> Vec<string>

Find all non-overlapping matches in the input string.

Returns an empty vector when there are no matches.

fn capture(self: Self, input: string, group: i64) -> Option<string>

Return the indexed capture from the first match.

Group 0 is the whole match. Returns None for no match, an invalid group, or a non-participating optional group.

fn capture_named(self: Self, input: string, name: string) -> Option<string>

Return the named capture from the first match.

Returns None for no match, unknown name, or non-participating optional group.

fn captures(self: Self, input: string) -> CaptureMatches

Return first-match submatches as one flat row.

fn find_all_submatch(self: Self, input: string) -> CaptureMatches

Return all matches' submatches as row-major flat results.

fn clone(self: Self) -> Pattern

Clone the compiled pattern, producing an independent copy.

Each clone must be dropped or freed independently.

fn free(self: Self)

Canonical release path for Pattern.

Callers MUST invoke free() before the value goes out of scope; dropping without free() is a resource leak.

Trait CaptureMatchesMethods

Methods available on flat capture results.

Methods

fn len(self: Self) -> i64

Return the number of match rows.

fn is_empty(self: Self) -> bool

Return true when there are no match rows.

fn width(self: Self) -> i64

Return the number of groups in each row, including group 0.

fn group(self: Self, match_index: i64, group_index: i64) -> Option<string>

Return a capture by match row and group index.

fn whole(self: Self, match_index: i64) -> Option<string>

Return group 0 for the requested match row.