Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| // <Snippet5> | |
| using System; | |
| using System.Diagnostics; | |
| using System.IO; | |
| using System.Text.RegularExpressions; | |
| public class Example | |
| { | |
| public static void Main() | |
| { | |
| string pattern = @"\b(\w+((\r?\n)|,?\s))*\w+[.?:;!]"; | |
| Stopwatch sw; | |
| Match match; | |
| int ctr; | |
| StreamReader inFile = new StreamReader(@".\Dreiser_TheFinancier.txt"); | |
| string input = inFile.ReadToEnd(); | |
| inFile.Close(); | |
| // Read first ten sentences with interpreted regex. | |
| Console.WriteLine("10 Sentences with Interpreted Regex:"); | |
| sw = Stopwatch.StartNew(); | |
| Regex int10 = new Regex(pattern, RegexOptions.Singleline); | |
| match = int10.Match(input); | |
| for (ctr = 0; ctr <= 9; ctr++) { | |
| if (match.Success) | |
| // Do nothing with the match except get the next match. | |
| match = match.NextMatch(); | |
| else | |
| break; | |
| } | |
| sw.Stop(); | |
| Console.WriteLine(" {0} matches in {1}", ctr, sw.Elapsed); | |
| // Read first ten sentences with compiled regex. | |
| Console.WriteLine("10 Sentences with Compiled Regex:"); | |
| sw = Stopwatch.StartNew(); | |
| Regex comp10 = new Regex(pattern, | |
| RegexOptions.Singleline | RegexOptions.Compiled); | |
| match = comp10.Match(input); | |
| for (ctr = 0; ctr <= 9; ctr++) { | |
| if (match.Success) | |
| // Do nothing with the match except get the next match. | |
| match = match.NextMatch(); | |
| else | |
| break; | |
| } | |
| sw.Stop(); | |
| Console.WriteLine(" {0} matches in {1}", ctr, sw.Elapsed); | |
| // Read all sentences with interpreted regex. | |
| Console.WriteLine("All Sentences with Interpreted Regex:"); | |
| sw = Stopwatch.StartNew(); | |
| Regex intAll = new Regex(pattern, RegexOptions.Singleline); | |
| match = intAll.Match(input); | |
| int matches = 0; | |
| while (match.Success) { | |
| matches++; | |
| // Do nothing with the match except get the next match. | |
| match = match.NextMatch(); | |
| } | |
| sw.Stop(); | |
| Console.WriteLine(" {0:N0} matches in {1}", matches, sw.Elapsed); | |
| // Read all sentnces with compiled regex. | |
| Console.WriteLine("All Sentences with Compiled Regex:"); | |
| sw = Stopwatch.StartNew(); | |
| Regex compAll = new Regex(pattern, | |
| RegexOptions.Singleline | RegexOptions.Compiled); | |
| match = compAll.Match(input); | |
| matches = 0; | |
| while (match.Success) { | |
| matches++; | |
| // Do nothing with the match except get the next match. | |
| match = match.NextMatch(); | |
| } | |
| sw.Stop(); | |
| Console.WriteLine(" {0:N0} matches in {1}", matches, sw.Elapsed); | |
| } | |
| } | |
| // The example displays the following output: | |
| // 10 Sentences with Interpreted Regex: | |
| // 10 matches in 00:00:00.0047491 | |
| // 10 Sentences with Compiled Regex: | |
| // 10 matches in 00:00:00.0141872 | |
| // All Sentences with Interpreted Regex: | |
| // 13,443 matches in 00:00:01.1929928 | |
| // All Sentences with Compiled Regex: | |
| // 13,443 matches in 00:00:00.7635869 | |
| // | |
| // >compare1 | |
| // 10 Sentences with Interpreted Regex: | |
| // 10 matches in 00:00:00.0046914 | |
| // 10 Sentences with Compiled Regex: | |
| // 10 matches in 00:00:00.0143727 | |
| // All Sentences with Interpreted Regex: | |
| // 13,443 matches in 00:00:01.1514100 | |
| // All Sentences with Compiled Regex: | |
| // 13,443 matches in 00:00:00.7432921 | |
| // </Snippet5> |