Mega Code Archive

 
Categories / C# Tutorial / Regular Expression
 

Match words that contain any letters in the range b through d

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 ="knife knock five";          Console.WriteLine("Matching words that contain any letters in the range 'b' through 'd'");     DisplayMatches(text, @"\S*[b-d]\S*");   } } Matching words that contain any letters in the range 'b' through 'd' using the following regular expression: \S*[b-d]\S* knock