std/regex

March 13, 2026 ยท View on GitHub

The std/regex module providing regular expression support based on POSIX regex.h.

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();
}

Struct Definitions

Regex

Represents a compiled regular expression.

struct Regex {
    // Internal handles
}

Match

Represents a successful regex match.

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

Methods

Regex Construction

MethodSignatureDescription
compileRegex::compile(pattern: char*) -> RegexCompiles a regex pattern with default flags.
compile_with_flagsRegex::compile_with_flags(pattern: char*, flags: int) -> RegexCompiles with custom POSIX flags.
destroydestroy(self)Frees the compiled regex.
MethodSignatureDescription
matchmatch(self, text: char*) -> boolReturns true if the pattern matches anywhere in text.
findfind(self, text: char*) -> Option<Match>Returns the first match including position and length.
countcount(self, text: char*) -> intReturns the number of non-overlapping matches.
splitsplit(self, text: char*) -> Vec<String>Splits the text by the pattern.

Match Access

MethodSignatureDescription
as_stringas_string(self) -> char*Returns a pointer to the start of the match.
endend(self) -> intReturns the index after the last character of the match.

Static Helper Functions

MethodSignatureDescription
regex_matchregex_match(pattern: char*, text: char*) -> boolQuick check for a match.
regex_findregex_find(pattern: char*, text: char*) -> Option<Match>Find first match.
regex_countregex_count(pattern: char*, text: char*) -> intCount all matches.
regex_splitregex_split(pattern: char*, text: char*) -> Vec<String>Split text by pattern.