Chapter 7. Regular Expressions

7.0 Introduction

The .NET Framework Class Library (FCL) includes the System.Text.RegularExpressions namespace, which is devoted to creating, executing, and obtaining results from regular expressions executed against a string.

Regular expressions take the form of a pattern that matches zero or more characters within a string. The simplest of these patterns, such as .* (which matches anything except newline characters) and [A-Za-z] (which matches any letter) are easy to learn, but more advanced patterns can be difficult to learn and even more difficult to implement correctly. Learning and understanding regular expressions can take considerable time and effort, but the work will pay off.

Note

Two books that will help you learn and expand your understanding of regular expressions are Michael Fitzgerald’s Introducing Regular Expressions and Jan Goyvaerts and Steven Levithan’s Regular Expressions Cookbook, both from O’Reilly.

Regular expression patterns can take a simple form—such as a single word or character—or a much more complex pattern. The more complex patterns can recognize and match such items as the year portion of a date, all of the <SCRIPT> tags in an ASP page, or a phrase in a sentence that varies with each use. The .NET regular expression classes provide a very flexible and powerful way to perform tasks such as recognizing text, replacing text within a string, and splitting up text into individual sections based on one or more complex delimiters. ...

Get C# 6.0 Cookbook, 4th Edition 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.