Skip to content

Introduction

Corvus.Regex.Ecma262 is a regular expression engine that answers as ECMAScript does. Where System.Text.RegularExpressions has its own grammar and its own meaning for \d, \w, \s, ., case folding and Unicode properties, this engine has ECMA-262's, so a pattern written for JavaScript, or taken from a JSON Schema, matches the same texts here as it does there.

This guide covers:

  • Setup: the one package, and what it brings.
  • Patterns and flags: the flags are ECMA-262's letters, not RegexOptions, and what each one does.
  • The edition and the version of Unicode: the grammar of ECMAScript 2020, and the Unicode tables a pattern reads.
  • Matching: the members you know from System.Text.RegularExpressions, and the allocation-free forms beside them.
  • Timeouts: the limit of time and of memory a match may take.
  • UTF-8 and UTF-16: the same patterns over strings and over ReadOnlySpan<byte>, with no transcoding.
  • The source generator: [GeneratedRegex] and what it generates.
  • Differences from .NET's Regex: what to expect when the two meet.

Setup

One package carries the library and the source generator:

dotnet add package Corvus.Regex.Ecma262

The library targets net10.0 and netstandard2.0. The vectorised searches and the generated search code need the .NET 10 or later runtime. The netstandard2.0 build is for .NET Framework, where the same patterns give the same answers by the plain paths. .NET 8 and .NET 9 are not supported.

using Corvus.Ecma262.RegularExpressions;

Building ahead of the first match

A Regex builds the automata its matches use, and the program that reports groups, when a match first needs them, so it pays nothing for what it never does. A regular expression made once and matched many times, such as a validator's, can build everything in the constructor instead, so that its first match is no slower than the rest:

Regex r = new(pattern, RegexFlags.Unicode, Regex.DefaultMatchTimeout, RegexEdition.Current, RegexUnicodeVersion.Default, RegexBuildOptions.Eager);

Regex.TryCreate takes the same option. A regular expression from the source generator needs neither: everything is built at build time (see the source generator).

Start-up under ReadyToRun

An application published with ReadyToRun runs most of the library from code compiled ahead of time, but not the matchers themselves. They are generic over the text's code unit (byte for UTF-8, char for UTF-16), and ReadyToRun compiles no instantiation of them over those types, so the JIT compiles them the first time a pattern is compiled or matched in the process. That is a one-time cost at start-up, and steady-state speed is unaffected. Native AOT compiles everything ahead of time and has no such cost.

Patterns and flags

A pattern is written as it would be in JavaScript, and the flags are the letters RegExp takes, as a string or as the RegexFlags enumeration:

Flag RegexFlags Meaning
d HasIndices Accepted for completeness; indices are always kept.
g Global Replace replaces every match; without it, the first, as String.prototype.replace does.
i IgnoreCase Canonicalize of the mode in force: simple case folding with u or v, toUpperCase without. Never the culture.
m Multiline ^ and $ at line terminators.
s DotAll The dot matches line terminators.
u Unicode The u flag grammar; matched by code point.
v UnicodeSets Set notation and properties of strings; matched by code point. u and v together is a syntax error, as in ECMA-262.
y Sticky A match only at the start position.
Regex a = new(@"\p{Script=Greek}+", "u");
Regex b = new(@"[[a-z]--[aeiou]]+", RegexFlags.UnicodeSets);
Regex c = new(@"^(?<year>\d{4})-(?<month>\d{2})$", "u");

With no flag the pattern is read with the Annex B grammar and matched by UTF-16 code unit, which is what ECMA-262 does with no flag. JSON Schema's reading, the u flag first and Annex B when that refuses, is two calls of Regex.TryCreate, which costs no exception and takes the match timeout, so no global default is needed:

TimeSpan timeout = TimeSpan.FromSeconds(1);
if (!Regex.TryCreate(pattern, RegexFlags.Unicode, timeout, out Regex? regex, out string? error)
    && !Regex.TryCreate(pattern, RegexFlags.None, timeout, out regex, out error))
{
    throw new InvalidOperationException(error);
}

A pattern that is not valid throws RegexParseException, with the reason and the offset. Every pattern has deterministic limits of size, and RegexPatternTooLargeException says when one is passed.

The edition and the version of Unicode

By default a pattern is read with today's grammar, ECMAScript 2025 (RegexEdition.Current), and with the tables of the version of Unicode that V8 carries, 17 today (RegexUnicodeVersion.Default). Either can be overridden when the pattern is compiled, each independently of the other.

JSON Schema 2020-12 names the grammar of ECMAScript 2020 (ECMA-262, 11th edition), which RegexEdition.Es2020 selects in place of the default. It refuses what later editions added, and nothing else: the d and v flags, modifier groups such as (?i:...), a group name used by two groups, and a property name or value that is not in the 2020 tables (such as \p{Script=Yezidi}). A pattern it accepts has the same answers as in today's grammar.

The version of Unicode is chosen apart from the edition: RegexUnicodeVersion.V13, V17 or V18, Latest (18 today) or Default, the version of the V8 the answers are held to (17 today). It decides the members of each \p{...}, the characters of a group name and the case folding of the i flag. Regex.UnicodeVersion reports the version a pattern was compiled with. Both settings are read when the pattern is compiled, so a match costs the same whichever is chosen.

// ECMAScript 2020 as its edition was published, with the Unicode of its year.
Regex r = new(pattern, RegexFlags.Unicode, timeout, RegexEdition.Es2020, RegexUnicodeVersion.V13);

The source generator takes them as named arguments: [GeneratedRegex(@"\p{Script=Greek}+", "u", Edition = RegexEdition.Es2020)].

Matching

The members are the ones of System.Text.RegularExpressions.Regex, with the same names and the same shapes:

Regex regex = new(@"(?<word>\w+)@(?<host>\w+)\.\w+", "gu");

bool any = regex.IsMatch("mail sherlock@baker.st today");
Match m = regex.Match("mail sherlock@baker.st today");
string host = m.Groups["host"].Value;                       // "baker"
MatchCollection all = regex.Matches("a@b.c and d@e.f");     // 2 matches
string replaced = regex.Replace("a@b.c", "$<word> at $<host>"); // "a at b"
string?[] parts = regex.Split("x a@b.c y");

Match, Groups, Matches, Replace with a string and Split with a string allocate what they return, as .NET's do: that is the drop-in shape. The span members beside them allocate nothing:

// Every match, with the groups into a span the caller gives.
Span<ValueCapture> groups = stackalloc ValueCapture[regex.GroupCount + 1];
foreach (ValueMatch match in regex.EnumerateMatches(text.AsSpan(), groups))
{
    ReadOnlySpan<char> host = text.AsSpan(groups[2].Index, groups[2].Length);
}

// One match from a position.
if (regex.TryMatch(text.AsSpan(), 0, groups, out ValueMatch first)) { }

// Replace into a span, or into a buffer writer.
Span<char> buffer = stackalloc char[256];
if (regex.TryReplace(text.AsSpan(), "$<word>".AsSpan(), buffer, out int written)) { }
regex.Replace(text.AsSpan(), "$<word>".AsSpan(), new ArrayBufferWriter<char>());

// Split without the array.
foreach (Range piece in regex.EnumerateSplits(text.AsSpan())) { }

int n = regex.Count(text);

Matches, EnumerateMatches and Count are the global search whether or not g is set. Where ECMA-262 keeps lastIndex on the RegExp, the library takes the position as an argument: Match(input, startAt) is exec with lastIndex set, and y anchors it there. A Regex has no mutable state, so one instance is safe to share between threads.

Timeouts

A match has no limit by default. A Regex made with a timeout (MatchTimeout) abandons a match that reaches it, or that reaches the cap on backtracking memory the timeout brings, with RegexMatchTimeoutException, whatever the pattern:

Regex slow = new(@"^(?:(a+)\1*)+$", RegexFlags.None, TimeSpan.FromMilliseconds(100));
try
{
    slow.IsMatch(new string('a', 60) + "b");
}
catch (RegexMatchTimeoutException)
{
}

An instance made without a timeout takes Regex.DefaultMatchTimeout: the REGEX_DEFAULT_MATCH_TIMEOUT data of the AppContext, which System.Text.RegularExpressions reads too, and no timeout when it is not set. A program that matches patterns or texts from outside should set one, for example with AppContext.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(1)) when it starts.

A pattern without a backreference is matched in time linear in the text, and no pattern can overflow the stack, so the timeout is what bounds the others.

UTF-8 and UTF-16

A string or a ReadOnlySpan<char> is UTF-16, lone surrogates and all, with positions in code units. Every member that takes a ReadOnlySpan<char> has a twin that takes UTF-8 bytes, and the engine matches the bytes as they are. Nothing is transcoded, and the positions it gives are byte offsets.

Regex regex = new(@"\p{L}+", "gu");
ReadOnlySpan<byte> utf8 = "Шерлок Холмс"u8;

int words = regex.Count(utf8);                                     // 2
foreach (ValueMatch m in regex.EnumerateMatches(utf8))
{
    ReadOnlySpan<byte> word = utf8.Slice(m.Index, m.Length);       // the bytes of one word
}

ArrayBufferWriter<byte> output = new();
regex.Replace(utf8, "_"u8, output);                                // "_ _"

Under the u and v flags a character is a code point in both encodings, so a class, a dot or a quantifier takes a four-byte sequence as one character. With no flag ECMA-262 matches by UTF-16 code unit; over UTF-8 the engine keeps that meaning, so a pattern with no flag gives the same answers over the bytes of a text as over its chars, except where the text holds a lone surrogate, which UTF-8 cannot hold.

UTF-8 is checked before it is read: text that is not UTF-8 throws ArgumentException, and is never matched or read as U+FFFD. The members whose names end in Unchecked skip the check, for text already checked.

The source generator

[GeneratedRegex] on a partial method or a partial property writes the matcher as code at build time, in the shape of .NET's own attribute (a partial property needs C# 13, which net10.0 has by default; on netstandard2.0 set LangVersion to 13 or use a method). The pattern and the flags are checked by the generator: a pattern that is not valid, a flag that is not one, or u with v, is a build error with the reason and the position.

public static partial class Patterns
{
    [GeneratedRegex(@"\b\d{1,2}/\d{1,2}/\d{2,4}\b", "gu")]
    public static partial Regex Dates();

    [GeneratedRegex(@"(?<word>\w+)", RegexFlags.Global | RegexFlags.Unicode | RegexFlags.IgnoreCase, 100)]
    public static partial Regex Words { get; }
}

int n = Patterns.Dates().Count("on 11/18/2019 and 1/2/03");       // 2

The member returns one Regex each time, built on the generated code. The generated code is the search for where a match can start and the match itself as structured code, over both encodings. A pattern too large for straight-line code (a class of thousands of strings, such as \p{RGI_Emoji}) is handed to the interpreter by its generated runner, so every pattern compiles.

The generator also writes the compiled regular expression into the generated code as data: the programs, the automaton that answers IsMatch over ASCII text, and the automata that speed a search, all worked out at build time. The Regex is made from that data with nothing parsed, compiled or built at run time, so its first match costs what a later one does, under the JIT, ReadyToRun and native AOT alike. The data records the version of the engine that wrote it; a library that is not that version ignores it and compiles the pattern, with the same answers.

The generator ships in the same package and runs on the netstandard2.0 build of the library, as a source generator must.

In generated code

A source generator does not run on another generator's output, so a [GeneratedRegex] member written by a code generator is never given its body. A code generator writes the body itself with the Corvus.Regex.Ecma262.CodeGeneration.Sources package: RegexEmitter.TryEmit returns the same code the source generator writes, as a class to nest in the generated type, or the reason the pattern does not compile. The package is C# compiled into the code generator, with no assembly, so a source generator can take it. It compiles into a project on .NET 10 or later, or on .NET Standard 2.0, which a source generator targets; a project on .NET 8 or 9 is refused with the error CRES0001.

<PackageReference Include="Corvus.Regex.Ecma262.CodeGeneration.Sources" PrivateAssets="all" />

Differences from .NET's Regex

The surface follows System.Text.RegularExpressions; the meaning follows ECMA-262. The differences a user coming from .NET will meet:

  • The grammar. No (?i) inline option (ECMA-262 has (?i:...) groups), no atomic groups, no possessive quantifiers, no \A and \z, no conditionals, no balancing groups. \z is the letter z under Annex B and an error under u.
  • The character classes. \d is [0-9], \w is the 63 ASCII word characters, \s is ECMAScript's white space and line terminators, and . excludes the four line terminators (LF, CR, U+2028, U+2029).
  • Case. With i and u, simple case folding (so ſ matches s and K matches k); with i and no flag, toUpperCase of one character. Never the current culture.
  • Captures. ECMA-262 keeps only a group's last capture under a quantifier, so Group.Captures has one entry.
  • Positions. Indices are in the units of the text matched: UTF-16 code units over strings, bytes over UTF-8.
  • Timeouts. Patterns without backreferences are linear and take no timeout; a timeout applies to the others, and the memory a match may use is bounded as well.