How to find a matching substring using regular expression in C#?

CsharpProgrammingServer Side Programming

Our string is −

string str = " My make ";

Use the following regular expression to find the substring “make”

@"\bmake\b"

Here is the complete code −

Example

Live Demo

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   public class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);

         foreach(Match m in mc) {
            Console.WriteLine("Found:" + m);
         }
      }

      public static void Main(string[] args) {
         string str = "My make";

         Console.WriteLine("Matching words start with 'm' and ends with 'e':");
         showMatch(str, @"\bmake\b");
      }
   }
}

Output

Matching words start with 'm' and ends with 'e':
The Expression: \bmake\b
Found:make
raja
Published on 20-Aug-2018 13:32:10
Advertisements