Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 2.25 KB

File metadata and controls

70 lines (52 loc) · 2.25 KB

Standard Library: Regex (std/regex.zc)

Regular expression support based on POSIX regex.h. Compile patterns, match strings, and extract sub-matches or split text.

Usage

import "std/regex.zc"

fn main() {
    if regex_match("^[a-z]+$", "hello") {
        println "Matches!";
    }
    
    let re = Regex::compile("\\d+");
    let count = re.count("123 abc 456");
    re.destroy();
}

Structure

struct Regex {
    // internal handles
}

struct Match {
    text: char*;
    start: int;
    len: int;
}

Methods

Regex Construction

Method Signature Description
compile Regex::compile(pattern: char*) -> Regex Compiles a regex pattern with default flags.
compile_with_flags Regex::compile_with_flags(pattern: char*, flags: int) -> Regex Compiles with custom POSIX flags.
destroy destroy(self) Frees the compiled regex.

Matching & Search

Method Signature Description
match match(self, text: char*) -> bool Returns true if the pattern matches anywhere in text.
find find(self, text: char*) -> Option<Match> Returns the first match including position and length.
count count(self, text: char*) -> int Returns the number of non-overlapping matches.
split split(self, text: char*) -> Vec<String> Splits the text by the pattern into a Vector of Strings.

Match Access

Method Signature Description
as_string as_string(self) -> char* Returns a pointer to the start of the match in the original text string.
end end(self) -> int Returns the index after the last character of the match.

Helper Functions

Convenience functions that compile, execute, and destroy in one step.

Function Signature Description
regex_match regex_match(pattern: char*, text: char*) -> bool Quick check for a match.
regex_find regex_find(pattern: char*, text: char*) -> Option<Match> Find first match.
regex_count regex_count(pattern: char*, text: char*) -> int Count all matches.
regex_split regex_split(pattern: char*, text: char*) -> Vec<String> Split text by pattern.