Regular Expression to search words with specified number of characters

Madhu.b.rokkam
Posted by Madhu.b.rokkam under C# category on | Points: 40 | Views : 2161
//Search words with 5 or more characters
Regex regex = new Regex("[a-zA-Z]{5,}");

Example

string sourceString = "We are Indians, we are Engineers, we are part of DNF ...";
//Regex regex = new Regex("are");
Regex regex = new Regex("[a-zA-Z]{5,}");

MatchCollection matchCol = regex.Matches(sourceString);
foreach (Match match in matchCol)
{
Console.WriteLine(string.Format("{0} is at {1} position of the string", match.Value.Trim(), match.Index));
}

output

Indians is at 7 position of the string
Engineers is at 23 position of the string

Comments or Responses

Login to post response