Skip to content

RegExp

Defined in regexp@1.3.1

Simple regular expression.

Currently it only supports patterns below: - Character classes: [xyz], [^xyz], ., \d, \D, \w, \W, \s, \S, \t, \r, \n, \v, \f, [\b], x|y - Assertions: ^, $ - Groups: (x) - Quantifiers: x*, x+, x?, x{n}, x{n,}, x{n,m}

This follows JavaScript but for one thing: where several alternatives match at one place, the longest wins. a|ab matched against ab gives ab; JavaScript gives a.

For what the pattern syntax above means, see mdn web docs: Regular expressions.

LIMITATION:

A character class holds single byte characters (U+0001..U+007F). UTF-8 writes a character from U+0080 upward as two or more bytes, and a Fix string holds no null character (U+0000).

Values

namespace RegExp::Matcher

find_all

Type: Std::Array Std::U8 -> RegExp::Matcher -> (Std::Array (Std::Array (Std::I64, Std::I64)), RegExp::Matcher)

matcher.find_all(bytes) finds every match in bytes, and reports the scanner as the search left it.

What it reports is what RegExp::find_all reports.

Parameters
  • bytes - The bytes to search.
  • matcher - The regular expression and its scanner.

find_from

Type: Std::I64 -> Std::Array Std::U8 -> RegExp::Matcher -> (Std::Option (Std::Array (Std::I64, Std::I64)), RegExp::Matcher)

matcher.find_from(from, bytes) finds the match in bytes that begins first at or after the byte position from, and reports the scanner as the search left it.

What it reports is what RegExp::find_from reports.

Parameters
  • from - The byte position the match has to begin at or after.
  • bytes - The bytes to search.
  • matcher - The regular expression and its scanner.

match_all

Type: Std::String -> RegExp::Matcher -> (Std::Array (Std::Array Std::String), RegExp::Matcher)

matcher.match_all(target) matches target against the regular expression, and reports the scanner as the match left it.

What it reports is what RegExp::match_all reports.

Parameters
  • target - The string to match against.
  • matcher - The regular expression and its scanner.

match_one

Type: Std::String -> RegExp::Matcher -> (Std::Result Std::ErrMsg (Std::Array Std::String), RegExp::Matcher)

matcher.match_one(target) matches target against the regular expression, and reports the scanner as the match left it.

What it reports is what RegExp::match_one reports.

Parameters
  • target - The string to match against.
  • matcher - The regular expression and its scanner.

replace_all

Type: Std::String -> Std::String -> RegExp::Matcher -> (Std::String, RegExp::Matcher)

matcher.replace_all(target, replacement) replaces every stretch of target the regular expression matches with replacement, and reports the scanner as the work left it.

What it reports is what RegExp::replace_all reports.

Parameters
  • target - The string to work over.
  • replacement - The text to put in place of every match.
  • matcher - The regular expression and its scanner.

namespace RegExp::RegExp

compile

Type: Std::String -> Std::String -> Std::Result Std::ErrMsg RegExp::RegExp

RegExp::compile(pattern, flags) compiles pattern into a regular expression. flags change behavior of regular expression matching. Currently only global flag ("g") is supported.

find_all

Type: [?it : Std::Iterator, Std::Iterator::Item ?it = Std::Array (Std::I64, Std::I64)] Std::Array Std::U8 -> RegExp::RegExp -> ?it

regexp.find_all(bytes) is an iterator over the matches in bytes, taken left to right, none of them overlapping another, each reported as where its groups stand. Each match is looked for when the iterator is advanced to it, so a program that stops early does not pay for the matches after.

A match is reported as one (begin, end) pair of byte positions per group, the whole match first. A group that captured nothing stands as (-1, -1). Where several matches begin at one place, the longest is taken, and after a match that holds no byte the search goes on from the next byte. The global flag ("g") makes no difference here.

A string's positions are those of its bytes, which get_bytes.pop_back gives.

Example:

let regexp = RegExp::compile("([a-z]+)([0-9]+)", "").as_ok;
let found = regexp.find_all("abc012 def345".get_bytes.pop_back).to_array;
assert_eq(|_|"", found, [[(0, 6), (0, 3), (3, 6)], [(7, 13), (7, 10), (10, 13)]])

Parameters
  • bytes - The bytes to search.
  • regexp - The regular expression.

find_from

Type: Std::I64 -> Std::Array Std::U8 -> RegExp::RegExp -> Std::Option (Std::Array (Std::I64, Std::I64))

regexp.find_from(from, bytes) finds the match in bytes that begins first at or after the byte position from, taking the longest of those that begin there, and reports where each of its groups stands, as find_all reports them. It reports none() where no match begins at or after from. A negative from counts as 0.

Example:

let regexp = RegExp::compile("[0-9]+", "").as_ok;
let found = regexp.find_from(4, "abc012 def345".get_bytes.pop_back);
assert_eq(|_|"", found, some([(4, 6)]))

Parameters
  • from - The byte position the match has to begin at or after.
  • bytes - The bytes to search.
  • regexp - The regular expression.

match_all

Type: Std::String -> RegExp::RegExp -> Std::Array (Std::Array Std::String)

regexp.match_all(target) matches target against regexp. All matching results will be returned including captured groups.

If the match against the regular expression fails, an empty array is returned.

This function is similar to String.matchAll() function of JavaScript.

match_one

Type: Std::String -> RegExp::RegExp -> Std::Result Std::ErrMsg (Std::Array Std::String)

regexp.match(target) matches target against regexp.

If the global flag ("g") is not set, it returns an array of the groups of the first match. Group 0 is a substring that matches the entire regular expression. Group 1 and beyond are the captured substrings in each group. If not captured, the group will be an empty string.

Example:

let regexp = RegExp::compile("[a-z]+([0-9]+)", "").as_ok;
let groups = regexp.match_one("abc012 def345").as_ok;
assert_eq(|_|"", groups, ["abc012", "012"])

If the global flag ("g") is set, all matching results will be returned, but captured groups will not be included.

Example:

let regexp = RegExp::compile("[a-z]+([0-9]+)", "g").as_ok;
let groups = regexp.match_one("abc012 def345").as_ok;
assert_eq(|_|"", groups, ["abc012", "def345"])

If the match against the regular expression fails, an error "NotMatch" is reported.

This function is similar to String.match() function of JavaScript.

matcher

Type: RegExp::RegExp -> RegExp::Matcher

regexp.matcher is the regular expression together with a scanner of its own.

A scanner works out what the automaton does with a byte the first time it reads that byte in that state, and keeps the answer. Matcher's matching functions hand the scanner back, so a program that matches many strings against one regular expression pays for the working out once, where RegExp's own matching functions make a scanner for each match and let it go.

Example:

let matcher = RegExp::compile("[a-z]+", "g").as_ok.matcher;
let (first, matcher) = matcher.match_all("abc def");
let (second, matcher) = matcher.match_all("ghi jkl");
assert_eq(|_|"", first, [["abc"], ["def"]]);;
assert_eq(|_|"", second, [["ghi"], ["jkl"]])

replace_all

Type: Std::String -> Std::String -> RegExp::RegExp -> Std::String

regexp.replace_all(target, replacement) matches target against regexp, and replace all matching substrings with replacement. If replacement contains $&, it is substituted with entire matched substring. If replacement contains $n where n is an integer, it is substituted with the captured group. If replacement contains $$, it is substituted with single $.

Example:

let regexp = RegExp::compile("(\\w\\w)(\\w)", "").as_ok;
let result = regexp.replace_all("abc def ijk", "$2$1");
assert_eq(|_|"", result, "cab fde kij")

This function is similar to String.replaceAll() function of JavaScript. Note that $', $`, $<Name> are not supported yet.

Types and aliases

namespace RegExp

Matcher

Defined as: type Matcher = unbox struct { ...fields... }

A compiled regular expression together with the scanner it has built so far. See RegExp::matcher.

field global

Type: Std::Bool

field dfa

Type: RegExp.RegExpNFA::DFA

Matches

Defined as: type Matches = unbox struct { ...fields... }

The matches RegExp::find_all reports, each looked for when the iterator is advanced to it.

RegExp

Defined as: type RegExp = unbox struct { ...fields... }

Type of a compiled regular expression.

field flags

Type: Std::String

field nfa

Type: RegExp.RegExpNFA::NFA

Traits and aliases

Trait implementations

impl RegExp::Matches : Std::Iterator