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
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_validA 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_snapshotOne result of re_findall: the matched text and a List of captured groups (a List handle stored as long).
testo str
gruppi refFunctions
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.
1 if group n (0 included) captured something in the last match.
Pointer to the text right after the last match; feed it back to re_finditer_next to continue scanning.
Pointer to the start of the last match inside the text, "" if none.
group(n) on a frozen MatchResult.is_group_valid(n) on a frozen MatchResult.Like re_sub but the replacement is computed by callback(match_text) for each occurrence.
Freezes the captures of the last match into a MatchResult owned by the caller; call it right after the match, before running another.
Method behind p.sub(repl, text).
Method behind p.match(text).
Method behind p.fullmatch(text).
Method behind p.search(text).
Method behind p.group(n).
Method behind p.is_valid(n).
Method behind p.snapshot().
Method behind p.sub_cb(callback, text).
Prepares a pattern once for reuse on many texts; returns a CompiledPattern.
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.
Compiles a pattern to the opcode program used by the VM (called by the search functions; exposed for tools).
1 if pattern matches at the start of text (like Python's re.match); the whole text need not be consumed.
1 if pattern matches the entire text.
1 if pattern matches anywhere in text (a leading ^ keeps it anchored).
The text of the next match scanning from the start of text, "" if none. Loop by advancing the text with match_rest().
The captured groups of the last match as a List of str (index 0 = group 1).
Every non-overlapping match as a List of ReMatch, each with its own snapshot of the groups.
Number of non-overlapping matches.
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.
- def atom_len(pattern: str) -> int
- def is_shorthand_letter(letter: char) -> int
- def escaped_literal_char(letter: char) -> char
- def is_space_char(c: char) -> int
- def is_word_char(c: char) -> int
- def shorthand_class_match(letter: char, c: char) -> int
- def class_fill(atom: str, alen: int) -> None
- inline def class_match(atom: str, alen: int, c: char) -> int
- def atom_match(atom: str, alen: int, c: char) -> int
- def ptr_diff(a: str, b: str) -> int
- def set_capture_text_base(t: str) -> int
- inline def mark_start_char() -> char
- inline def mark_end_char() -> char
- def reset_captures() -> int
- def record_group_start(n: int, pos: str) -> int
- def record_group_end(n: int, pos: str) -> int
- def find_close_paren(pattern: str, open_at: int, stop: int) -> int
- def find_alt_in_range(pattern: str, start: int, stop: int) -> int
- def seq_consumes_one(pattern: str, start: int, stop: int) -> int
- def copy_block(from_pc: int, to_pc: int) -> int
- def emit_opt_group(body_from: int) -> int
- def emit_star_loop(body_from: int) -> int
- def unroll_group(head: int, rmin: int, rmax: int) -> int
- def compile_range(pattern: str, start: int, stop: int) -> int
- def vm_group_start(n: int) -> int
- def vm_group_end(n: int) -> int
- inline def vm_atom_at(pc: int, c: char, pattern: str) -> int
- def vm_run(pc: int, t: str, pattern: str) -> int
- def clear_if_failed(r: int) -> int
- def build_class_lut(pattern: str, pc: int) -> int
- def backref_group_num(repl: str, i: int) -> int
- def repl_expanded_len(repl: str) -> int
- def apply_repl_backrefs(repl: str) -> str