15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

egular expressions ❘ 219<br />

Suppose your application needed to convert U.S. phone numbers to an international format. In the United<br />

States, the phone numbers have this format: 314 - 123 - 1234, which is often written as (314) 123 - 1234. When<br />

converting this national format to an international format you have to include +1 (the country code of the<br />

United States) <strong>and</strong> add brackets around the area code: +1 (314) 123 - 1234. As fi nd - <strong>and</strong> - replace operations<br />

go, that is not too complicated. It would still require some coding effort if you were going to use the String<br />

class for this purpose (which would mean that you would have to write your code using the methods<br />

available from System.String ). The regular expressions language allows you to construct a short string<br />

that achieves the same result.<br />

This section is intended only as a very simple example, so it concentrates on searching strings to identify<br />

certain substrings, not on modifying them.<br />

The regularexpressionsPlayaround example<br />

For the rest of this section, you develop a short example, called RegularExpressionsPlayaround, that<br />

illustrates some of the features of regular expressions <strong>and</strong> how to use the .<strong>NET</strong> regular expressions engine in<br />

<strong>C#</strong> by performing <strong>and</strong> displaying the results of some searches. The text you are going to use as your sample<br />

document is an introduction to a Wrox Press book on ASP.<strong>NET</strong> ( Professional ASP.<strong>NET</strong> 4: in <strong>C#</strong> <strong>and</strong> VB,<br />

Wiley Publishing, 2010, ISBN 978 - 0 - 470 5-0220-4).<br />

const string myText =<br />

@"This comprehensive compendium provides a broad <strong>and</strong> thorough investigation of all<br />

aspects of programming with ASP.<strong>NET</strong>. Entirely revised <strong>and</strong> updated for the fourth<br />

release of .<strong>NET</strong>, this book will give you the information you need to<br />

master ASP.<strong>NET</strong> <strong>and</strong> build a dynamic, successful, enterprise Web application.";<br />

code snippet RegularExpressionsPlayaround.cs<br />

This code is valid <strong>C#</strong> code, despite all the line breaks. It nicely illustrates the utility of<br />

verbatim strings that are prefi xed by the @ symbol.<br />

This text is referred to as the input string. To get your bearings <strong>and</strong> get used to the regular expressions of<br />

.<strong>NET</strong> classes, you start with a basic plain text search that does not feature any escape sequences or regular<br />

expression comm<strong>and</strong>s. Suppose that you want to fi nd all occurrences of the string ion . This search string<br />

is referred to as the pattern. Using regular expressions <strong>and</strong> the Text variable declared previously, you could<br />

write this:<br />

const string pattern = "ion";<br />

MatchCollection myMatches = Regex.Matches(myText, pattern,<br />

RegexOptions.IgnoreCase |<br />

RegexOptions.ExplicitCapture);<br />

foreach (Match nextMatch in myMatches)<br />

{<br />

Console.WriteLine(nextMatch.Index);<br />

}<br />

This code uses the static method Matches() of the Regex class in the System.Text.RegularExpressions<br />

namespace. This method takes as parameters some input text, a pattern, <strong>and</strong> a set of optional fl ags taken<br />

from the RegexOptions enumeration. In this case, you have specifi ed that all searching should be case -<br />

insensitive. The other fl ag, ExplicitCapture , modifi es the way that the match is collected in a way that, for<br />

your purposes, makes the search a bit more effi cient — you see why this is later (although it does have other<br />

uses that we won ’ t explore here). Matches() returns a reference to a MatchCollection object. A match<br />

is the technical term for the results of fi nding an instance of the pattern in the expression. It is represented<br />

by the class System.Text.RegularExpressions.Match . Therefore, you return a MatchCollection that<br />

contains all the matches, each represented by a Match object. In the preceding code, you simply iterate over<br />

the collection <strong>and</strong> use the Index property of the Match class, which returns the index in the input text of<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!