regex
The regex module provides an implementation of regular expressions which adheres
closely to the POSIX Extended Regular Expressions (ERE) specification[0]. This
implementation computes matches in linear time.
By default, matches will be found anywhere in the given string. The ^ and $
characters can be used to anchor the match to the beginning or end of the
string.
find() returns a slice of captures for the first match. The first
capture represents the entire matching string, while the rest represent the
matching substrings for the subexpressions, specified in the regular expression
using parentheses.
findall() finds all non-overlapping matches in the given string and returns
a slice of slices of captures.
This module implements the POSIX match disambiguation rules by returning
the longest match among the leftmost matches.
const re = regex::compile(`[Hh]are`)!;
defer regex::finish(&re);
const does_match = regex::test(&re, "Hello Hare, hello Hare.");
fmt::printfln("matched? {}", does_match)!;
const first_match = regex::find(&re, "Hello Hare, hello Hare.");
match (first_match) {
case void => void;
case let captures: []regex::capture =>
defer regex::free_captures(captures);
// captures[0]: The full matching string.
// captures[1...]: A capture for every capture group.
fmt::printfln("{} ({}, {})", captures[0].content,
captures[0].start,
captures[0].end)!;
};
const all_matches = regex::findall(&re, "Hello Hare, hello Hare.");
match (all_matches) {
case void => void;
case let matches: [][]regex::capture =>
defer regex::free_matches(matches);
// matches[0]: All captures for the first match.
// matches[0][0]: The full matching string for the first match.
// matches[0][1...]: A capture for every capture group in the
// first match.
for (let i = 0z; i < len(matches); i += 1) {
fmt::printfln("{} ({}, {})", matches[i][0].content,
matches[i][0].start,
matches[i][0].end)!;
};
};
[
0]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04
Index
Types
type capture;
type charclass;
type charset;
type charset_class_item;
type charset_lit_item;
type charset_range_item;
type inst;
type inst_any;
type inst_charset;
type inst_groupend;
type inst_groupstart;
type inst_jump;
type inst_lit;
type inst_match;
type inst_repeat;
type inst_skip;
type inst_split;
type regex;
Errors
type error;
Functions
fn compile(str) (regex | error);
fn find(*regex, str) (void | []capture);
fn findall(*regex, str) (void | [][]capture);
fn finish(*regex) void;
fn free_captures([]capture) void;
fn free_matches([][]capture) void;
fn strerror(error) str;
fn test(*regex, str) bool;
Types
type capture
type capture = struct {
content: str,
start: size,
end: size,
};
A (sub)match found as a result of matching a certain string against a regex.
type charclass
Show undocumented member
type charclass = enum {
ALNUM,
ALPHA,
BLANK,
CNTRL,
DIGIT,
GRAPH,
LOWER,
PRINT,
PUNCT,
SPACE,
UPPER,
XDIGIT,
};
type charset
Show undocumented member
type charset = [](charset_lit_item | charset_range_item | charset_class_item);
type charset_class_item
Show undocumented member
type charset_class_item = charclass;
type charset_lit_item
Show undocumented member
type charset_lit_item = rune;
type charset_range_item
Show undocumented member
type charset_range_item = (u8, u8);
type inst
Show undocumented member
type inst = (inst_lit | inst_any | inst_split | inst_jump | inst_skip | inst_match |
inst_charset | inst_groupstart | inst_groupend | inst_repeat);
type inst_any
Show undocumented member
type inst_any = void;
type inst_charset
Show undocumented member
type inst_charset = struct {
idx: size,
is_positive: bool,
};
type inst_groupend
Show undocumented member
type inst_groupend = void;
type inst_groupstart
Show undocumented member
type inst_groupstart = void;
type inst_jump
Show undocumented member
type inst_jump = size;
type inst_lit
Show undocumented member
type inst_lit = rune;
type inst_match
Show undocumented member
type inst_match = bool;
type inst_repeat
Show undocumented member
type inst_repeat = struct {
id: size,
origin: size,
min: (void | size),
max: (void | size),
};
type inst_skip
Show undocumented member
type inst_skip = void;
type inst_split
Show undocumented member
type inst_split = size;
type regex
Show undocumented member
type regex = struct {
insts: []inst,
charsets: []charset,
n_reps: size,
};
Errors
type error
type error = !str;
A string describing the error the occurred.
Functions
fn compile
fn compile(expr: str) (regex | error);
Compiles a string containing a regular expression into a regex struct.
fn find
fn find(re: *regex, string: str) (void | []capture);
Attempts to match a regular expression against a string and returns the
longest leftmost match, or void if there is no match.
fn findall
fn findall(re: *regex, string: str) (void | [][]capture);
Attempts to match a regular expression against a string and returns all
non-overlapping matches, or void if there are no matches.
fn finish
fn finish(re: *regex) void;
Frees the memory used by a regex.
fn free_captures
fn free_captures(s: []capture) void;
Frees a slice of captures.
fn free_matches
fn free_matches(s: [][]capture) void;
Frees each match in a slice of matches, as well as the slice itself.
fn strerror
fn strerror(err: error) str;
Converts regex error into a user-friendly string.
fn test
fn test(re: *regex, string: str) bool;
Returns whether or not a regex matches a string.