15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

but a match on the final word data would display g of data. (only one character after the match), because<br />

after that you get to the end of the string. This longer string lets you see more clearly where the regular<br />

expression locates the match:<br />

static void WriteMatches(string text, MatchCollection matches)<br />

{<br />

Console.WriteLine("Original text was: \n\n" + text + "\n");<br />

Console.WriteLine("No. of matches: " + matches.Count);<br />

}<br />

foreach (Match nextMatch in matches)<br />

{<br />

int index = nextMatch.Index;<br />

string result = nextMatch.ToString();<br />

int charsBefore = (index < 5) index: 5;<br />

int fromEnd = text.Length-index-result.Length;<br />

int charsAfter = (fromEnd < 5) fromEnd: 5;<br />

int charsToDisplay = charsBefore + charsAfter + result.Length;<br />

}<br />

Console.WriteLine("Index: {0}, \tString: {1}, \t{2}",<br />

index, result,<br />

text.Substring(index-charsBefore, charsToDisplay));<br />

The bulk of the processing in this method is devoted to the logic of figuring out how many characters in the<br />

longer substring it can display without overrunning the beginning or end of the input text. Note that you<br />

use another property on the Match object, Value, which contains the string identified for the match. Other<br />

than that, RegularExpressionsPlayaround simply contains a number of methods with names such as<br />

Find1, Find2, <strong>and</strong> so on, which perform some of the searches based on the examples in this section. For<br />

example, Find2 looks for any string that contains a at the beginning of a word:<br />

static void Find2()<br />

{<br />

string text = @"This comprehensive compendium provides a broad <strong>and</strong> thorough<br />

investigation of all aspects of programming with ASP.<strong>NET</strong>. Entirely revised <strong>and</strong><br />

updated for the 3.5 Release of .<strong>NET</strong>, this book will give you the information<br />

you need to master ASP.<strong>NET</strong> <strong>and</strong> build a dynamic, successful, enterprise Web<br />

application.";<br />

string pattern = @"\ba";<br />

MatchCollection matches = Regex.Matches(text, pattern,<br />

RegexOptions.IgnoreCase);<br />

WriteMatches(text, matches);<br />

}<br />

Along with this comes a simple Main() method that you can edit to select one of the Find() methods:<br />

static void Main()<br />

{<br />

Find1();<br />

Console.ReadLine();<br />

}<br />

The code also needs to make use of the RegularExpressions namespace:<br />

using System;<br />

using System.Text.RegularExpressions;<br />

Running the example with the Find2() method shown previously gives these results:<br />

RegularExpressionsPlayaround<br />

Original text was:<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 3.5<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!