Regex
Struct · namespace System · Since 0.21
struct RegexRegular expressions: matching, searching, replacing and splitting text.
var re = try Regex.Create("(?<year>\\d{4})-(\\d{2})-(\\d{2})");if (re.Match("due 2026-10-01!") is RegexMatch m) Console.WriteLine(m.Value + " " + m.Group("year") + " " + m.Group(2)); // 2026-10-01 2026 10string s = re.Replace("2026-10-01", "$3.$2.${year}"); // 01.10.2026foreach (var word in (try Regex.Create("\\s*,\\s*")).Split("a , b,c")) // a b c Console.WriteLine(word);The syntax (a subset of .NET’s and Perl’s):
| Pattern | Matches |
|---|---|
x |
the character; \. \* \\ … a character that is special otherwise; \n \r \t \f \v \0 \xHH \uHHHH |
. |
any character but \n |
[abc] [a-z] [^0-9] |
a character of the set, or not of it ([\d_], [\w-] work too) |
\d \w \s |
a digit, a word character [A-Za-z0-9_], white space; \D \W \S the opposite |
^ $ |
the start and end of the text (of each line with (?m)); \A \z always of the text |
\b \B |
a word boundary, not a word boundary |
(x) |
a group (numbered from 1); (?<name>x) a named group; (?:x) a group that captures nothing |
x|y |
x or y |
x* x+ x? x{n} x{n,} x{n,m} |
repetition; with a ? after it (x*?, x+?, …) as few times as possible |
(?i) (?m) (?im) |
at the start of the pattern: ignore case (ASCII letters), ^ and $ match at line breaks |
Backreferences (\1) and lookaround are not supported. The text is UTF-8: ., a set and \w etc. match one
character (code point), positions and lengths are in bytes. The matcher is a backtracking one that remembers which
states it has tried, so a search takes at most (pattern size) x (text length) steps, also for patterns like
(a*)*.
Fields
Section titled “Fields”| Name | Description |
|---|---|
Pattern |
The pattern the regex was created from. |
Pattern
Section titled “Pattern”string PatternThe pattern the regex was created from.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Create |
The pattern compiled. |
Create
Section titled “Create”static RegexError<Regex> Create(string pattern)The pattern compiled.
Errors
RegexError.InvalidPattern: the pattern is not valid; the message says what is wrong and where.
Methods
Section titled “Methods”| Name | Description |
|---|---|
IsMatch |
Whether the pattern occurs somewhere in text. |
Match |
The first match in text. |
Matches |
Every match in text, from left to right, without overlaps. |
Replace |
text with every match replaced by replacement. |
Split |
The parts of the text between the matches (empty matches do not split). |
IsMatch
Section titled “IsMatch”bool IsMatch(string text)Whether the pattern occurs somewhere in text.
Optional<RegexMatch> Match(string text)The first match in text.
Returns nothing (null) if there is none.
Optional<RegexMatch> Match(string text, int start)The first match in text at the byte position start or after it.
Returns nothing (null) if there is none.
Matches
Section titled “Matches”RegexMatch[] Matches(string text)Every match in text, from left to right, without overlaps.
Replace
Section titled “Replace”string Replace(string text, string replacement)text with every match replaced by replacement. In the replacement $0 is the match, $1 … $9 and
${n} its groups, ${name} a named group, $$ a $.
string[] Split(string text)The parts of the text between the matches (empty matches do not split).