Microsoft .NET is the programming framework that serves as the foundation for programming languages such as Visual Basic, C#, F#, and PowerShell. All of these languages can use the .NET Regex class, which provides a very feature-rich regex flavor. The only noteworthy features that are lacking are possessive quantifiers and subroutine calls.
There are no differences in the regex syntax supported by the .NET Framework versions 2.0 through 4.8. There are no differences between this syntax and the syntax supported by any version of .NET Core either. That includes the original .NET Core 1.0.0 and the latest .NET 9.0.
The .NET regex engine has been updated with newer versions of Unicode throughout the years, which affects the characters matched by Unicode categories. But since the regex syntax hasn’t changed, .NET still supports only the Unicode blocks that existed in Unicode 4.0.1.
This section and the next use the Visual Basic syntax to illustrate how to use .NET’s RegularExpressions namespace. All of this is of course equally available in other .NET languages such as C#, F#, and PowerShell. The topics about the specific languages have code examples in each of those languages.
The regex classes are located in the namespace System.Text.RegularExpressions. To make them available, place Imports System.Text.RegularExpressions at the start of your source code.
The Regex class is the one you use to compile a regular expression. For efficiency, regular expressions are compiled into an internal format. If you plan to use the same regular expression repeatedly, construct a Regex object as follows: Dim RegexObj as Regex = New Regex("regularexpression"). You can then call RegexObj.IsMatch("subject") to check whether the regular expression matches the subject string. The Regex allows an optional second parameter of type RegexOptions. You could specify RegexOptions.IgnoreCase as the final parameter to make the regex case insensitive. Other options are IgnorePatternWhitespace which makes the regex free-spacing, RegexOptions.Singleline which makes the dot to match newlines, RegexOptions.Multiline which makes the caret and dollar to match at embedded newlines in the subject string, and RegexOptions.ExplicitCapture which turns all unnamed groups into non-capturing groups.
Call RegexObj.Replace("subject", "replacement") to perform a search-and-replace using the regex on the subject string, replacing all matches with the replacement string. In the replacement string, you can use $& to insert the entire regex match into the replacement text. You can use 1ドル, 2ドル, 3ドル, etc. to insert the text matched between capturing parentheses into the replacement text. Use $$ to insert a single dollar sign into the replacement text. To replace with the first backreference immediately followed by the digit 9, use ${1}9. If you type 19,ドル and there are less than 19 backreferences, then 19ドル will be interpreted as literal text, and appear in the result string as such. To insert the text from a named capturing group, use ${name}. Improper use of the $ sign may produce an undesirable result string, but will never cause an exception to be raised.
RegexObj.Split("Subject") splits the subject string along regex matches, returning an array of strings. The array contains the text between the regex matches. If the regex contains capturing parentheses, the text matched by them is also included in the array. If you want the entire regex matches to be included in the array, simply place parentheses around the entire regular expression when instantiating RegexObj.
The Regex class also contains several static methods that allow you to use regular expressions without instantiating a Regex object. This reduces the amount of code you have to write, and is appropriate if the same regular expression is used only once or reused seldomly. Note that member overloading is used a lot in the Regex class. All the static methods have the same names (but different parameter lists) as other non-static methods.
Regex.IsMatch("subject", "regex") checks if the regular expression matches the subject string. Regex.Replace("subject", "regex", "replacement") performs a search-and-replace. Regex.Split("subject", "regex") splits the subject string into an array of strings as described above. All these methods accept an optional additional parameter of type RegexOptions, like the constructor.
If you want more information about the regex match, call Regex.Match() to construct a Match object. If you instantiated a Regex object, use Dim MatchObj as Match = RegexObj.Match("subject"). If not, use the static version: Dim MatchObj as Match = Regex.Match("subject", "regex").
Either way, you will get an object of class Match that holds the details about the first regex match in the subject string. MatchObj.Success indicates if there actually was a match. If so, use MatchObj.Value to get the contents of the match, MatchObj.Length for the length of the match, and MatchObj.Index for the start of the match in the subject string. The start of the match is zero-based, so it effectively counts the number of characters in the subject string to the left of the match.
If the regular expression contains capturing parentheses, use the MatchObj.Groups collection. MatchObj.Groups.Count indicates the number of capturing parentheses. The count includes the zeroth group, which is the entire regex match. MatchObj.Groups(3).Value gets the text matched by the third pair of parentheses. MatchObj.Groups(3).Length and MatchObj.Groups(3).Index get the length of the text matched by the group and its index in the subject string, relative to the start of the subject string. MatchObj.Groups("name") gets the details of the named group "name".
To find the next match of the regular expression in the same subject string, call MatchObj.NextMatch() which returns a new Match object containing the results for the second match attempt. You can continue calling MatchObj.NextMatch() until MatchObj.Success is False.
Note that after calling RegexObj.Match(), the resulting Match object is independent from RegexObj. This means you can work with several Match objects created by the same Regex object simultaneously.
Passing RegexOptions.ECMAScript to the Regex() constructor changes the behavior of certain regex features to follow the behavior prescribed in the ECMA-262 standard. This standard defines the ECMAScript language, which is better known as JavaScript. The table below compares the differences between canonical .NET (without the ECMAScript option) and .NET in ECMAScript mode.
For reference the table also compares how JavaScript in modern browsers behaves in these areas, both with and without the /u flag. This flag did not exist in JavaScript when .NET’s regex engine was first released. This flag cleans up JavaScript’s regex syntax in addition to enabling Unicode features. Unfortunately, .NET has not kept up with this. As a result, in some ways, .NET’s ECMAScript mode makes its regex flavor less compatible with JavaScript in /u mode.
| Feature or Syntax | Canonical .NET | .NET in ECMAScript mode | JavaScript without /u | JavaScript with /u |
|---|---|---|---|---|
| RegexOptions.ExplicitCapture | Supported | Only via (?n) | Not supported | |
| RegexOptions.FreeSpacing | Supported | Only via (?x) | Not supported | |
| RegexOptions.SingleLine | Supported | Only via (?s) | /s | |
| Escaped letter or underscore that does not form a regex token | Error | Literal letter or underscore | Error | |
| Escaped digit that is not a valid backreference | Error | Octal escape or literal 8 or 9 | Error | |
| Escaped double digits that do not form a valid backreference | Error | Single digit backreference and literal digit if the single digit backreference is valid; otherwise single or double digit octal escape and/or literal 8 and 9 | Error | |
| Backreference to non-participating group | Fails to match | Zero-length match | ||
| Forward reference | Supported | Error | Zero-length match | |
| Backreference to group 0 | Fails to match | Zero-length match | Syntax error | |
| \s | Unicode | ASCII | Unicode | |
| \d | Unicode | ASCII | ||
| \w | Unicode | ASCII | ||
| \b | Unicode | ASCII | ||
| Feature or Syntax | Canonical .NET | .NET in ECMAScript mode | JavaScript without /u | JavaScript with /u |
Though RegexOptions.ECMAScript brings the .NET regex engine a little bit closer to JavaScript’s, there are still significant differences between the .NET regex flavor and the JavaScript regex flavor. When creating web pages using ASP.NET on the server and JavaScript on the client, you cannot assume the same regex to work in the same way both on the client side and the server side even when setting RegexOptions.ECMAScript. The next table lists the more important differences between .NET and JavaScript. RegexOptions.ECMAScript has no impact on any of these.
| Feature or syntax | .NET | JavaScript without /u | JavaScript with /u |
|---|---|---|---|
| Unicode properties | Categories and blocks | None | Categories, scripts, and binary properties |
| Astral characters | Broken into surrogate pairs | Properly supported | |
| \u{10FFFF} | Not supported | Supported | |
| Dot | [^\n] | [^\n\r\u2028\u2029] | |
| Anchors in multi-line mode | Treat only \n as a line break | Treat \n, \r, \u2028, and \u2029 as line breaks | |
| $ without multi-line mode | Matches at very end of string | Matches before final line break and at very end of string | |
| Permanent start and end of string anchors | Supported | Not supported | |
| Empty character class | Syntactically not possible | Fails to match | |
| Mode modifiers | Toggles and groups | Groups only | |
| Comments | Supported | Not supported | |
| Named capture and backreferences | Syntax with angle brackets and single quotes | Syntax with angle brackets only | |
| Numbers for named capturing groups | After unnamed groups | Mixed with unnamed groups | |
| Numbers as group names | Allowed | Syntax error | |
| Balancing groups | Supported | Not supported | |
| Conditionals | Supported | Not supported | |
| Feature or syntax | .NET | JavaScript without /u | JavaScript with /u |
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| grep | PowerGREP | RegexBuddy | RegexMagic |
| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |
| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |
| Google BigQuery | MySQL | Oracle | PostgreSQL |
Page URL: https://www.regular-expressions.info/dotnet.html
Page last updated: 13 October 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.