C# Essentials – Regular Expression in .NET


  In this blog you will learn how to define and execute regular expression, both for finding and replacing the content within string of data .

Table of content:

  • Finding patterns
    •    IsMatch(), Match(), Matches()
  • Replacing content
    •               Replace()
Finding Patterns


 The IsMatch function Is used to determine the content of string matches the expression.  

Include System.Text.RegularExpressions namespace to use Regular expression. Let’s start by using regular expression to find content within string.

 Regex CapWord = new(@"[A-Z]\w+");

The above statement creates an expression that looks for uppercase character followed by non-whitespace characters.

The @ symbol outside the string says it is a sting literal content, which means if we use back slash the compiler will consider we are not escaping something that’s a real back slash.

 string testStr = "Hello, How Are You?";
            string testStr1 = "hello, how are you?";
            Regex CapWord = new(@"[A-Z]\w+");
 var result  =  CapWord.IsMatch(testStr);
            var result1 =  CapWord.IsMatch(testStr1);
            Console.WriteLine($"{testStr} - Statment having caps character ? {result}");
            Console.WriteLine($"{testStr1} - Statment having caps character ? {result1}");

If we execute the above program using a .NET Console application. The output of result will be true because the string (contains caps lock character) matches the expression and output of result1 will be false.   


Match ()

Match method returns a single match at a time.

var result2 = CapWord.Match(testStr); //Match method returns a single match at a time
            while (result2.Success)
                Console.WriteLine($"{result2.Value} found at position: {result2.Index}");
                result2 = result2.NextMatch();

The match will return the actual match. In the string we have three capitalized words based on the expression so we can iterate over each one of them.


The matches return a collection of match object.

            var mc = CapWord.Matches(testStr);
            Console.WriteLine($" No of mactched words:  {mc.Count}");
            foreach (Match match in mc)
                Console.WriteLine($"{match.Value} found at position: {match.Index}");

The above return the collection of matched objects, we have iterated the collections to print the value and index of matched word.  

Replacing content


 Regular expression can be used to replace the content in string.

            var replacedStr = CapWord.Replace(testStr, "***");
            Console.WriteLine($"Replaced string {replacedStr}");

 The above statement will replace the matched word (Capitalized word) with asterisks.


Replacement text can be generated on the fly using MatchEvaluator

            string upperStr = CapWord.Replace(testStr, new MatchEvaluator(MakeUpper));
            Console.WriteLine($"Replaced content on the fly: {upperStr}");

MakeUpper function will be invoked by MatchEvaluator to convert the matched word into upper case.

          string MakeUpper(Match m)
                string s = m.ToString();

                return s.ToUpper();

The above statement will replace the Matched word to Upper case



We have seen how to start with regular expression in .NET with C# and we learned how to define and execute regular expression, both for finding and replacing the content within string of data.

Source code – Get here.

I hope this blog will help you to get start with Regular Expression in .NET. Please share your queries, suggestions in the comments section below.

Happy Coding!!!


Leave a Reply

%d bloggers like this: