Chapter 13: Regular Expressions


[R]egular expression culture is a mess, and I share some of the blame for making it that way. Since my mother always told me to clean up my own messes, I suppose I'll have to do just that.
Larry Wall, Apocalypse 5 (Perl 6)

Overview

Regular expressions provide an extremely powerful, concise, and expressive way to work with and process text. Processing text might sound a tad boring, but this task has historically been and continues to be a fundamental requirement of administrative scripts or utility applications. Often, one's ability to hack together a simple yet useful program depends entirely on one's ability to effectively crunch and operate on textual input. UNIX and the Perl language, for example, have pioneered the use of regular expressions for decades proving the technology solid and making it ubiquitous.

This chapter will walk you through the primary facets of the regular expression syntax and demonstrate the rich support available in the .NET Framework. All of the types we will work with in this chapter come from the System.Text.RegularExpressions namespace, found in the System.dll assembly. What you will learn about the syntax of regular expressions will be readily transferable to other technologies. In fact, that's one of the great things about the technology: it's everywhere (and you may not even realize it until you learn more about it).

Regular expression syntax is relatively compact. The ability to combine the simple pieces of the language together using powerful means of combination is where the technology really shines. Unfortunately, writability and readability are sometimes at odds with one another. The result can be some very-difficult-to-maintain expressions, especially for those not as proficient with regular expressions as the author was. But once you learn the fundamentals, it becomes relatively easy to break expressions up into their smaller building blocks, which themselves are easier to understand. Nonetheless, it's not uncommon to crack open a source file in which you wrote an overly clever regular expression six months ago and be dumbfounded for the first 30 minutes of trying to figure out what it does. (Especially if there's a subtle bug in it.)




Professional. NET Framework 2.0
Professional .NET Framework 2.0 (Programmer to Programmer)
ISBN: 0764571354
EAN: 2147483647
Year: N/A
Pages: 116
Authors: Joe Duffy

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net