Regex

Source: lib/Regex.cs

This page is generated from the source by scripts/gen-reference.py.

Regex: regular expressions, compiled into an Arena, matched in linear time.

Regex date = Regex.Create(arena, @"(\d{4})-(\d\d)-(\d\d)");
RegexMatch m = date.Match(line);
if (m.Success) Console.WriteLine($"year: {m.Groups[1].Value}");
foreach (RegexMatch number in Regex.Create(arena, @"\d+").Matches(text)) ...
string swapped = date.Replace(arena, line, "$3/$2/$1");
foreach (ReadOnlySpan<char> field in Regex.Create(arena, @"\s*,\s*").Split(line)) ...

A subset of .NET's syntax, with .NET's results where the two overlap: literals and escapes (\t \n \r \f \v \e \xHH \uHHHH, \ before punctuation); . and classes ([a-z], [^...], \d \w \s and their negations, ASCII only); anchors ^ \(\A \z \Z \b \B; capturing groups (...), named ones (?<name>...) and (?'name'...), numbered after the others as in .NET, and (?:...); |; and the quantifiers * + ? {n} {n,} {n,m}, greedy or lazy (with a ? after). Options: IgnoreCase (ASCII letters), Multiline (^ and\) at every line), Singleline (. matches \n too). Not supported, and refused with an error: backreferences (\1), lookaround ((?=...) and the like), numbered names ((?<2>...)), inline options, atomic groups. At most 15 capturing groups; names are ASCII letters, digits and _.

The pattern is compiled into a small program that a Pike VM runs: every possible way of matching advances through the input together, one character at a time. So a match takes time proportional to the input's length times the program's, whatever the pattern: no pattern can make it backtrack for ages, as (a+)+b can in a backtracking engine. When several matches start at the same place, the one chosen is the one .NET's engine would find first.

Replace takes .NET's substitutions: $1, ${1} and $ for a group, $$ for a $, $& for the match, $` and $' for the input before and after it, $+ for the last group and $_ for the whole input; anything else after a $ is kept as it is. It writes into an arena, a StrBuilder or a ScratchText you pass, or calls a function you give for each match. Split gives the pieces between matches as slices of the input, with the text of any groups in between, as .NET's Split does.

Create allocates the program and the memory matching works in, in the arena you pass; matching and Split allocate nothing. That working memory is shared, so use a Regex from one thread at a time. An invalid pattern ends the program, as .NET throws ArgumentException; TryCreate returns the reason instead.

Members

Create

public static Regex Create(
    Arena arena,
    ReadOnlySpan<char> pattern,
    RegexOptions options = RegexOptions.None)

Compiles pattern into arena. Ends the program with the reason if the pattern isn't valid (see TryCreate), or if the arena is full.

TryCreate

public static bool TryCreate(
    Arena arena,
    ReadOnlySpan<char> pattern,
    RegexOptions options,
    out Regex regex,
    out string error,
    out int errorPosition)

Compiles pattern into arena, or returns false with the reason and the position in the pattern it was found at (-1 for the pattern as a whole, or for a full arena).

IsCreated

public bool IsCreated

GroupCount

public int GroupCount

The number of capturing groups, not counting group 0, the whole match.

GroupNumberFromName

public int GroupNumberFromName(ReadOnlySpan<char> name)

The number of the group named name, or -1 if there is none.

GroupNameFromNumber

public string GroupNameFromNumber(int number)

The name of group number, or null if it has none (or doesn't exist). .NET gives an unnamed group's number as text instead.

IsMatch

public bool IsMatch(ReadOnlySpan<char> input)

Whether the pattern matches anywhere in input.

Match

public RegexMatch Match(ReadOnlySpan<char> input, int start = 0)

The first match at or after start, with its groups. Check Success; NextMatch() finds the one after it.

Matches

public RegexMatches Matches(ReadOnlySpan<char> input)

For foreach: every match, left to right, not overlapping.

Count

public int Count(ReadOnlySpan<char> input)

How many matches there are, not overlapping.

Replace

public string Replace(
    Arena arena,
    ReadOnlySpan<char> input,
    ReadOnlySpan<char> replacement,
    int count = -1)

input with each match replaced by replacement ($1, ${1}, $$, $&, $, $', $+, $_), as a new string in arena; the first count` matches only, if it isn't -1. Null if the arena is out of memory.

public bool Replace(
    StrBuilder output,
    ReadOnlySpan<char> input,
    ReadOnlySpan<char> replacement,
    int count = -1)

The same, appended to output. False if it isn't created or its arena is out of memory.

public void Replace(
    ScratchText output,
    ReadOnlySpan<char> input,
    ReadOnlySpan<char> replacement,
    int count = -1)

The same, appended to output, inside Arena.Scratch.

public string Replace(
    Arena arena,
    ReadOnlySpan<char> input,
    delegate*<RegexMatch, StrBuilder, void> evaluator,
    int count = -1)

input with each match replaced by what evaluator appends to the builder it is given, as a new string in arena, like .NET's MatchEvaluator:

static void Double(RegexMatch m, StrBuilder output)
{
   Str.TryParse(m.Value, out long number);
   output.Append(2 * number);
}
string doubled = numbers.Replace(arena, text, &Double);

Null if the arena is out of memory.

public bool Replace(
    StrBuilder output,
    ReadOnlySpan<char> input,
    delegate*<RegexMatch, StrBuilder, void> evaluator,
    int count = -1)

The same, appended to output. False if it isn't created or its arena is full.

Split

public RegexSplit Split(ReadOnlySpan<char> input, int count = 0)

For foreach: the pieces of input between matches, as slices of it, with the text of each group that took part in a match between the pieces before and after it, as .NET's Split. With a count above 0, at most that many pieces: the last is the rest of the input.

RegexOptions

RegexMatch

A match, or its absence (Success false). It refers to the input it was found in, so it lives only as long as that span.

Success

public bool Success

Index

public int Index

Where the match starts in the input (0 if there is none).

Length

public int Length

Value

public ReadOnlySpan<char> Value

The matched text, a slice of the input (empty if there is none).

Groups

public RegexGroups Groups

Group 0 is the whole match; group n, the n-th ( of the pattern.

NextMatch

public RegexMatch NextMatch()

The next match after this one. After an empty match, the search starts one character further on, so the loop always ends.

RegexGroups

A match's groups: match.Groups[1].Value.

this[]

public RegexGroup this[int number]

Group number; Success false if it didn't take part, or doesn't exist.

public RegexGroup this[ReadOnlySpan<char> name]

The group named name; Success false if it didn't take part, or doesn't exist.

RegexGroup

One group of a match.

Success

public bool Success

Index

public int Index

Length

public int Length

Value

public ReadOnlySpan<char> Value

RegexMatches

For foreach over Regex.Matches.

GetEnumerator

public Enumerator GetEnumerator()

RegexSplit

For foreach over Regex.Split: the pieces of the input, and the text of groups between them.

GetEnumerator

public Enumerator GetEnumerator()

For foreach: the pieces, in order.