Chapter 26. Regular Expressions

The regular expressions language identifies character patterns. The .NET types supporting regular expressions are based on Perl 5 regular expressions and support both search and search/replace functionality.

Regular expressions are used for tasks such as:

  • Validating text input such as passwords and phone numbers (ASP.NET provides the RegularExpressionValidator control just for this purpose)

  • Parsing textual data into more structured forms (e.g., extracting data from an HTML page for storage in a database)

  • Replacing patterns of text in a document (e.g., whole words only)

This chapter is split into both conceptual sections teaching the basics of regular expressions in .NET and reference sections describing the regular expressions language.

All regular expression types are defined in System.Text.RegularExpressions.


For more on regular expressions, is a good online reference with lots of examples, and Mastering Regular Expressions by Jeffrey E. F. Friedl, is invaluable for the serious programmer.

The samples in this chapter are all preloaded into LINQPad. There is also an interactive utility available called Expresso ( which assists in building and visualizing regular expressions, and comes with its own expression library.

Regular Expression Basics

One of the most common regular expression operators is a quantifier. ? is a quantifier that matches the preceding item 0 or 1 time. In other words, ? means ...

Get C# 5.0 in a Nutshell, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.