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.

224 ❘ ChaPTer 9 strinGs And reGulAr expressiOns<br />

that the group in question should not be saved (you only want to save 4355; you don’t need :4355 as well!).<br />

Don’t get confused by the two colons following each other — the first colon is part of the : sequence that<br />

says “don’t save this group,” <strong>and</strong> the second is text to be searched for.<br />

If you run this pattern on the following string, you’ll get one match: http://www.wrox.com.<br />

Hey I've just found this amazing URI at<br />

http:// what was it --oh yes http://www.wrox.com<br />

Within this match, you will find the three groups just mentioned as well as a fourth group, which represents<br />

the match itself. Theoretically, it is possible that each group itself might return no, one, or more than one<br />

match. Each of these individual matches is known as a capture. So, the first group, (\S+), has one capture,<br />

http. The second group also has one capture (www.wrox.com). The third group, however, has no captures,<br />

because there is no port number on this URI.<br />

Notice that the string contains a second http://. Although<br />

this does match up to the first group, it will not be captured<br />

by the search because the entire search expression does not<br />

match this part of the text.<br />

There isn’t space to show examples of <strong>C#</strong> code that uses<br />

groups <strong>and</strong> captures, but you should know that the .<strong>NET</strong><br />

RegularExpressions classes support groups <strong>and</strong> captures<br />

through classes known as Group <strong>and</strong> Capture. Also,<br />

the GroupCollection <strong>and</strong> CaptureCollection classes<br />

represent collections of groups <strong>and</strong> captures. The Match<br />

class exposes the Groups property, which returns<br />

the corresponding GroupCollection object. The Group<br />

class correspondingly implements the Captures property,<br />

which returns a CaptureCollection. The relationship<br />

between the objects is shown in Figure 9-3.<br />

You might not want to return a Group object every time<br />

you just want to group some characters. A fair amount of<br />

overhead is involved in instantiating the object, which is<br />

not necessary if all you want is to group some characters as<br />

part of your search pattern. You can disable this by starting<br />

the group with the character sequence : for an individual<br />

group, as was done for the URI example, or for all groups<br />

by specifying the RegExOptions.ExplicitCaptures flag<br />

on the RegEx.Matches() method, as was done in the<br />

earlier examples.<br />

RegEx<br />

figure 9-3<br />

Matches( )<br />

method<br />

Match( )<br />

method<br />

MatchCollection<br />

Match<br />

GroupCollection<br />

Group<br />

CaptureCollection<br />

Capture<br />

summary<br />

You have quite a number of available data types at your disposal when working with the .<strong>NET</strong> Framework.<br />

One of the most used types in your applications (especially applications that focus on the submission <strong>and</strong><br />

retrieval of data) is the string data type. The importance of string is the reason that this book has a<br />

complete chapter focused on how to use the string data type <strong>and</strong> manipulate it in your applications.<br />

When working with strings in the past, it was quite common to just slice <strong>and</strong> dice the strings as needed<br />

using concatenation. With the .<strong>NET</strong> Framework, you can use the StringBuilder class to accomplish a lot<br />

of this task with better performance than before.<br />

Last, but hardly least, advanced string manipulation using regular expressions is an excellent tool to search<br />

through <strong>and</strong> validate your strings.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!