XML technologies / Regular expressions / .NET / The regex dialect in .NET

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

.NET uses a traditional NFA, so the key lessons from How regex engines work, Practical regex techniques and The art of writing regular expressions apply. The following table summarizes the dialect, mostly covered under Features and dialects.

Some dialect features can be controlled by mode modifiers (see Regex modes): options in constructors and functions enable or disable features, while the constructs ˹(?mods-mods)˼ and ˹(?mods-mods:...)˼ enable modes inside the regex. Supported modifiers appear in the table .NET matching modes and modifiers.

The table shows bare metasequences such as ˹\w˼. Use them directly in VB.NET string literals ("\w") and the verbatim strings of C# (@"\w"). Languages without regex-friendly strings, such as C++, require doubling each backslash ("\\w"). See also Strings as regular expressions.

 

Table: Overview of .NET regex elements.

Character shorthands 1.

see Shorthand metacharacters

(K)

\a [\b] \e \f \n \r \t \v \oktal \x## \u#### \cZeichen

Character classes and related constructs

see Ordinary classes: [a-z] and [^a-z]

Classes:2. [...] [^...]

see Almost any character: dot

Any character except newline: dot (sometimes every character)

see Character-class shorthands

(K)

Character-class shorthands:3. \w \d \s \W \D \S

see Unicode properties, scripts, and blocks

(K)

Unicode properties and blocks:4. \p{Prop} \P{Prop}

Anchors and other zero-width assertions

see Start of line, start of string

  

Start of line or string: ^ \A

see End of line, end of string

  

End of line or string: $ \z \Z

see Start of the next search or end of the previous match: \G

  

End of the previous match:5. \G

see Word boundaries

  

Word boundary: \b \B

see Lookahead; lookbehind

  

Lookaround:6. (?=...) (?!...) (?<=...) (?<!...)

Comments and mode modifiers

see mode modifiers

  

Mode modifiers: (?mod-mod) Allowed modifiers: x s m i n (see .NET matching modes and modifiers)

see Scoped mode modifiers

  

Scope for mode modifiers: (?mod-mod:...)

see Comments

Comments: (?#...)

Grouping and capturing text

see Grouping and capturing parentheses

  

Capturing parentheses:7. (...) \1 \2 ...

see Matching nested constructs

Nested constructs: (?<Name-Name>...)

see Named capturing groups

Named captures and backreferences: (?<Name>...) \k<Name>

see Noncapturing grouping parentheses

  

Noncapturing grouping parentheses: (?:...)

see Atomic parentheses

  

Atomic groups: (?>...)

see Alternation

  

Alternation: |

see Greedy quantifiers

  

Greedy quantifiers: * + ? {n} {n,} {x,y}

see Lazy quantifiers

  

Lazy quantifiers: *? +? ?? {n}? {n,}? {x,y}?

see Conditional groups

Conditional regular expressions: (? if then|else) – »if« can be lookaround, (Num) or (Name).

(K) – also usable in a character class

1....7. see text

Notes on the table:

  1. Inside a character class, \b means backspace; outside it means word boundary (see Word boundaries).

    With \x##, exactly two hexadecimal digits are required; ˹\xFCber˼ matches ›über‹.

    With \u####, exactly four hexadecimal digits are required; ˹\u00FCber˼ matches ›über‹, and ˹\u20AC˼ matches ›€‹.

  2. Since version 2.0, .NET supports character-class subtraction such as ˹[a-z-[aeiou]]˼ for ASCII consonants (see Simple class subtraction: [[a-z]-[aeiou]]). Inside a class, minus followed by an inner class subtracts its characters from the outer class.

  3. \w, \d and \s, and uppercase variants, normally match Unicode characters, but the option RegexOptions.ECMAScript restricts them to ASCII (see ECMAScript mode).

    Normally, the class \w includes the Unicode properties \p{Ll}, \p{Lu}, \p{Lt}, \p{Lo}, \p{Nd} and \p{Pc}. Note that \p{Lm} is excluded. See the property list in Basic Unicode subproperties.

    Normally, \s is equivalent to ˹[● \f\n\r\t\v\x85\p{Z}]˼. U+0085 is Unicode’s NEXT LINE control character, and \p{Z} contains Unicode separators (see Unicode properties, scripts, and blocks).

  4. The constructs \p{...} and \P{...} support most Unicode 4.0.1 properties and blocks, but not scripts.

    Block names require the prefix ›Is‹ (see Unicode properties, scripts, and blocks across languages), in canonical form without spaces or underscores. Neither \p{Is_Greek_Extended} nor \p{Is Greek Extended} is allowed; only \p{IsGreekExtended} is allowed.

    Only short property names such as \p{Lu} are supported, not full names such as \p{Lowercase_Letter}. Even single-letter properties require braces: \pL cannot be used to abbreviate \p{L}. See the tables Unicode properties: basic concepts and Basic Unicode subproperties.

    The special compound property \p{L&} and properties \p{Pi} and \p{Pf} are unsupported. The special properties \p{All}, \p{Assigned} and \p{Unassigned} are also unsupported, but can be replaced by ˹(?s:.)˼, ˹\P{Cn}˼ and ˹\p{Cn}˼.

  5. \G refers to the end of the previous match, although the documentation claims it refers to the start of the current match (see Start of the next search or end of the previous match: \G).

  6. Lookahead and lookbehind allow arbitrary regexes. When I wrote this chapter, .NET was the only known regex engine supporting arbitrary-length lookbehind (see Lookahead; lookbehind).

  7. The option RegexOptions.ExplicitCapture, or mode modifier (?n), makes ordinary parentheses ˹(...)˼ noncapturing. Only explicitly named groups such as ˹(?<num>\d+)˼ then capture text (see Named subexpressions). Named captures therefore let you replace the unsightly grouping-only parentheses ˹(?...)˼ with ˹(...)˼ .

 

Table: .NET matching modes and modifiers.

RegexOptions option

(?mod)

Description

.Singleline

s

dot matches every character (see Dot-matches-all mode).

.Multiline

m

˹^˼ and ˹$˼ also match internal line starts and ends.

.IgnorePatternWhitespace

x

Free-spacing mode and comments.

.IgnoreCase

i

Treats uppercase and lowercase as equivalent.

.ExplicitCapture

n

Ordinary parentheses ˹(...)˼ no longer capture; only named groups ˹(?<Name>...)˼ do.

.ECMAScript

The character classes ˹\w˼, ˹\s˼ and ˹\d˼ become ASCII-only (see ECMAScript mode).

.RightToLeft

The transmission tries each position in reverse, from the string’s end toward its start. This option has bugs (see Right-to-left matching).

.Compiled

Optimizes the regex during compilation for faster matching (see Optimized expressions with Compiled).

  

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top