libstd/re

A regular-expression engine with the architecture of CPython's re: the pattern is compiled to opcodes and run by a backtracking VM. Greedy * + ?, {m,n}, anchors, classes and shorthands (\d \w \s), capturing and non-capturing groups, alternation, lookahead, backreferences in the pattern and in the replacement. Captures of the last match are read with group(n); compile_pattern gives a reusable object with methods.

#include <libstd/re>

Includes libstd/string, libstd/type, libstd/list, libstd/stdio, libstd/dict.

Example

#include <libstd/stdio>
#include <libstd/re>
#include <libstd/list>

if re_search("([a-z]+)=([0-9]+)", "x=42, y=7"):
    print(group(0), " ", group(1), " ", group(2), "\n")      // x=42 x 42

print(re_sub("(\\w+) (\\w+)", "\\2 \\1", "ciao mondo"), "\n")   // mondo ciao

all = re_findall("[0-9]+", "a1 b22 c333")
m = list_get(all, 1 as long) as ReMatch
print(list_length(all), " ", m.testo, "\n")                  // 3 22

pat = compile_pattern("^[0-9]{3}-[0-9]{4}$")
print(pat.fullmatch("555-1234"), "\n")                     // 1

Classes

class
MatchResult

A frozen copy of the captures of one match (see snapshot_match); m.get(n) and m.is_valid(n) read it.

    texts List
    valid int[33]
    get &match_result_group
    is_valid &match_result_is_valid
class
CompiledPattern

A pattern prepared once and reused: p.match(text), p.fullmatch(text), p.search(text), p.sub(repl, text), p.sub_cb(cb, text), p.group(n), p.is_valid(n), p.snapshot().

    pattern str
    match &pattern_match
    fullmatch &pattern_fullmatch
    search &pattern_search
    sub &pattern_sub
    sub_cb &pattern_sub_cb
    group &pattern_group
    is_valid &pattern_is_valid
    snapshot &pattern_snapshot
class
ReMatch

One result of re_findall: the matched text and a List of captured groups (a List handle stored as long).

    testo str
    gruppi ref

Functions

def group(n: int) -> str

The text captured by group n (1–9) in the last match; group(0) is the whole match. "" if the group does not exist or did not participate.

def is_group_valid(n: int) -> int

1 if group n (0 included) captured something in the last match.

def match_rest() -> str

Pointer to the text right after the last match; feed it back to re_finditer_next to continue scanning.

def match_start() -> str

Pointer to the start of the last match inside the text, "" if none.

def match_result_group(self: MatchResult, n: int) -> str
group(n) on a frozen MatchResult.
def match_result_is_valid(self: MatchResult, n: int) -> int
is_group_valid(n) on a frozen MatchResult.
def re_sub_cb(pattern: str, callback: &(str) -> str, text: str) -> str

Like re_sub but the replacement is computed by callback(match_text) for each occurrence.

def snapshot_match() -> MatchResult

Freezes the captures of the last match into a MatchResult owned by the caller; call it right after the match, before running another.

def pattern_sub(self: CompiledPattern, replace: str, text: str) -> str

Method behind p.sub(repl, text).

def pattern_match(self: CompiledPattern, text: str) -> int

Method behind p.match(text).

def pattern_fullmatch(self: CompiledPattern, text: str) -> int

Method behind p.fullmatch(text).

def pattern_search(self: CompiledPattern, text: str) -> int

Method behind p.search(text).

def pattern_group(self: CompiledPattern, n: int) -> str

Method behind p.group(n).

def pattern_is_valid(self: CompiledPattern, n: int) -> int

Method behind p.is_valid(n).

def pattern_snapshot(self: CompiledPattern) -> MatchResult

Method behind p.snapshot().

def pattern_sub_cb(self: CompiledPattern, callback: &(str) -> str, text: str) -> str

Method behind p.sub_cb(callback, text).

def compile_pattern(pattern: str) -> CompiledPattern

Prepares a pattern once for reuse on many texts; returns a CompiledPattern.

def re_unsupported(pattern: str) -> int

Prints a pattern that re_compile cannot compile (bad syntax, an unsupported construct such as lookbehind, or a pattern over the engine's limits) and ends the program; it never returns. The search functions call it.

def re_compile(pattern: str, want_eol: int) -> int

Compiles a pattern to the opcode program used by the VM (called by the search functions; exposed for tools).

def re_match(pattern: str, text: str) -> int

1 if pattern matches at the start of text (like Python's re.match); the whole text need not be consumed.

def re_fullmatch(pattern: str, text: str) -> int

1 if pattern matches the entire text.

def re_search(pattern: str, text: str) -> int

1 if pattern matches anywhere in text (a leading ^ keeps it anchored).

def re_finditer_next(pattern: str, text: str) -> str

The text of the next match scanning from the start of text, "" if none. Loop by advancing the text with match_rest().

def re_groups() -> List

The captured groups of the last match as a List of str (index 0 = group 1).

def re_findall(pattern: str, text: str) -> List

Every non-overlapping match as a List of ReMatch, each with its own snapshot of the groups.

def re_count_matches(pattern: str, text: str) -> int

Number of non-overlapping matches.

def re_sub(pattern: str, repl: str, text: str) -> str

Replaces every non-overlapping match with repl, which may contain \1–\9 backreferences to that match's groups.

Internal helpers

Used by the functions above; not meant to be called directly.