How to create a pattern for a word that starts with letter M using Regex in C#

1 Answer

0 votes
using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main() {
        string pattern = @"\b[M]\w+";  
        Regex regx = new Regex(pattern);  
          
        string s = "Python, Mike, Java, Memory, C, C++, Mcaronis, C#, Mac, PHP, MATLAB";  
        
        MatchCollection math = regx.Matches(s);  
        
        for (int count = 0; count < math.Count; count++)  
            Console.WriteLine(math[count].Value);  
    }
}




/*
run:

Mike
Memory
Mcaronis
Mac
MATLAB

*/

 



answered Dec 20, 2021 by avibootz
...