Appendix T Regular Expressions

This appendix summarizes methods of creating and using regular expressions.

Creating Regular Expressions

This section describes the characters you can use to define regular expressions.

Character Escapes

Character escapes are character sequences that match special characters. The following table summarizes useful character escapes.

EscapeMeaning
\tMatches the tab character
\rMatches the return character
\nMatches the new-line character
\nnnMatches a character with ASCII code given by the two or three octal digits nnn
\xnnMatches a character with ASCII code given by the two hexadecimal digits nn
\unnnnMatches a character with Unicode representation given by the four hexadecimal digits nnnn

Character Classes

A character class matches one of a set of characters. The following table summarizes useful character class constructs.

ConstructMeaning
[chars]Matches one of the characters inside the brackets. For example, [aeiou] matches a single lowercase vowel.
[^chars]Matches a character that is not inside the brackets. For example, [^aeiouAEIOU] matches a single nonvowel character such as Q, ?, or 3.
[first-last]Matches a character between the character first and the character last. For example, [a–z] matches any lowercase letter between a and z. You can combine multiple ranges as in [a-zA-Z], which matches uppercase or lowercase letters.
.This is a wildcard that matches any single character except \n. (To match a period, use the

Get C# 5.0 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.