Section 15.3. Regular Expressions


15.3. Regular Expressions

As noted earlier, regular expressions provide a very powerful way to describe and manipulate text through pattern matching.

The result of applying a regular expression to a string is either to return a substring or to return a new string representing a modification of some part of the original string. (Remember that string objects are immutable and so cannot be changed by the regular expression.)

By applying a properly constructed regular expression to the following string:

 One,Two,Three Liberty Associates, Inc. 

you can return any or all of its substrings (such as "Liberty" or "One") or modified versions of its substrings (such as "LIBeRtY" or "OnE"). What the regular expression does is determined by the syntax of the regular expression itself.

A regular expression consists of two types of characters : literals and metacharacters . A literal is a character you want to match in the target string. A metacharacter is a special symbol that acts as a command to the regular expression parser. The parser is the engine responsible for understanding the regular expression. For example, if you create a regular expression:

 ^(FromToSubjectDate): 

this will match any substring with the letters "From," "To," "Subject," or "Date," so long as those letters start a new line ( ^ ) and end with a colon (:).

The caret ( ^ ) indicates to the regular expression parser that the string you're searching for must begin a new line. The letters "From" and "To" are literals, and the metacharacters left and right parentheses ( ( , ) ) and vertical bar ( ) are all used to group sets of literals and indicate that any of the choices should match. Thus, you would read the following line as "match any string that begins a new line, followed by any of the four literal strings From, To, Subject, or Date, and followed by a colon":

 ^(FromToSubjectDate): 

A full explanation of regular expressions is beyond the scope of this book, but all the regular expressions used in the examples are explained. For a complete understanding of regular expressions, I highly recommend Mastering Regular Expressions , Second Edition, by Jeffrey E. F. Friedl (O'Reilly, 2002).




Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

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