[ Team LiB ] |
6.5 Procedural- and Expression-Based PatternsGenerally, users of regular expressions fall into one of two groups. The first group tends to use minimal regular expressions that provide matching or grouping behaviors, and then write procedural code to perform some iterative behavior. The second group tries to utilize the maximum power and functionality of the expression-processing engine itself, with as little procedural logic as possible. For most of us, the best answer is somewhere in between. We'll now explore the trade-offs in complexity and performance of these two approaches. 6.5.1 Procedural-Based PatternsA common processing need is to match certain parts of a string and perform some processing. So, here's an example that matches words within a string and capitalizes them: using System; using System.Text.RegularExpressions; class ProceduralFun { static void Main( ) { string txt = "the quick red fox jumped over the lazy brown dog."; Console.WriteLine("text=["+txt+"]"); string res = ""; string pat = @"\w+|\W+"; // Loop through all the matches foreach (Match m in Regex.Matches(txt, pat)) { string s = m.ToString( ); // If the first char is lower case, capitalize it if (char.IsLower(s[0])) s = char.ToUpper(s[0])+s.Substring(1, s.Length-1); res += s; // Collect the text } Console.WriteLine("result=["+res+"]"); } } As you can see, you use the C# foreach statement to process the set of matches found, and perform some processing (in this case, creating a new result string). The output of the sample is: text=[the quick red fox jumped over the lazy brown dog.] result=[The Quick Red Fox Jumped Over The Lazy Brown Dog.] 6.5.2 Expression-Based PatternsAnother way to implement the previous example is by providing a MatchEvaluator, which processes it as a single result set. So the new sample looks like: using System; using System.Text.RegularExpressions; class ExpressionFun { static string CapText(Match m) { // Get the matched string string s = m.ToString( ); // If the first char is lower case, capitalize it if (char.IsLower(s[0])) return char.ToUpper(s[0]) + s.Substring(1, s.Length-1); return s; } static void Main( ) { string txt = "the quick red fox jumped over the lazy brown dog."; Console.WriteLine("text=[" + txt + "]"); string pat = @"\w+"; MatchEvaluator me = new MatchEvaluator(CapText); string res = Regex.Replace(txt, pat, me); Console.WriteLine("result=[" + res + "]"); } } Also of note is that the pattern is simplified, since we need only to modify the words, not the nonwords. |
[ Team LiB ] |