Procedural- and Expression-Based Patterns

Generally, 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.

Procedural-Based Patterns

A 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 ...

Get C# in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.