Skip to content

Regex

Spice
import "std/text/regex";

Match struct

A single match of a Regex against an input string: the whole-match text and bounds, plus one entry per capturing group in groups (in group-number order, i.e. groups[0] is group 1) - an empty Optional means that group did not participate in the match (e.g. it was on the untaken side of an alternation).

Fields

Name Type Description
value String
startIndex unsigned long
endIndex unsigned long
groups Vector<Optional<String>>

Regex struct

A compiled regex pattern. Construct one with compileRegex.

Supported syntax: literals, ., character classes ([abc], [^abc], [a-z]), the shorthand classes \d \D \w \W \s \S, anchors ^ $, the quantifiers * + ? {n} {n,} {n,m} (greedy only), alternation |, and groups (...) / (?:...). Lazy quantifiers, backreferences, lookaround and multiline mode are not supported.

Matching runs on a Thompson-construction virtual machine (see "std/text/regex/regex-matcher"), not backtracking, so no pattern can trigger catastrophic-backtracking blowup: matching a pattern of m compiled instructions against an input of length n is always O(n * m).

Methods

isMatch

Spice
public f<bool> Regex.isMatch(const String& input)

Check whether the pattern matches anywhere in the input string

Parameters

Name Type Description
input const String& String to search

Returns: bool — Whether the pattern matches anywhere in input

find

Spice
public f<Optional<Match>> Regex.find(const String& input, unsigned long startIndex = 0l)

Find the first match at or after startIndex

Parameters

Name Type Description
input const String& String to search
startIndex unsigned long Position in input to start searching from (default: 0l)

Returns: Optional<Match> — The first match at or after startIndex, or an empty Optional if there is none

findAll

Spice
public f<Vector<Match>> Regex.findAll(const String& input)

Find every non-overlapping match in the input string, left to right

Parameters

Name Type Description
input const String& String to search

Returns: Vector<Match> — Every non-overlapping match, in order

replace

Spice
public f<String> Regex.replace(const String& input, const String& replacement)

Replace the first match with replacement. replacement may reference capture groups with $0 (the whole match) through $9; $$ inserts a literal $.

Parameters

Name Type Description
input const String& String to search
replacement const String& Replacement text, possibly containing $0-$9 backreferences

Returns: Stringinput with its first match, if any, replaced

replaceAll

Spice
public f<String> Regex.replaceAll(const String& input, const String& replacement)

Replace every non-overlapping match with replacement. replacement may reference capture groups with $0 (the whole match) through $9; $$ inserts a literal $.

Parameters

Name Type Description
input const String& String to search
replacement const String& Replacement text, possibly containing $0-$9 backreferences

Returns: Stringinput with every non-overlapping match replaced

split

Spice
public f<Vector<String>> Regex.split(const String& input)

Split the input string at every non-overlapping, non-empty match

Parameters

Name Type Description
input const String& String to split

Returns: Vector<String> — The pieces of input between matches, including a leading/trailing empty piece where input starts/ends with a match

Functions

compileRegex

Spice
public f<Result<Regex>> compileRegex(const String& pattern)

Compile a regex pattern.

Parameters

Name Type Description
pattern const String& Pattern to compile

Returns: Result<Regex> — The compiled regex, or an error describing the first problem encountered in the pattern