Mega Code Archive

 
Categories / C# Tutorial / Regular Expression
 

Match words that start with s and end with e

using System; using System.Text.RegularExpressions; class MainClass {   private static void DisplayMatches(string text,string regularExpressionString) {     Console.WriteLine("using the following regular expression: " +regularExpressionString);     MatchCollection myMatchCollection = Regex.Matches(text, regularExpressionString);     foreach (Match myMatch in myMatchCollection) {       Console.WriteLine(myMatch);     }   }   public static void Main() {     string text ="end main void static start she";          Console.WriteLine("Matching words that start with 's' and end with 'e'");     DisplayMatches(text, @"\bs\S*e\b");   } } Matching words that start with 's' and end with 'e' using the following regular expression: \bs\S*e\b she