Using regular expression to search for string,substring among array of strings

//Below is an function which searches for a existence of a string,substring among array of strings.

//Declare the string to search and the content string

 
string[] words ={"This","is an attempt","to search","string contents","using","regular expressions." };
string strToSearch = "regular";

//call the function SearchAndDisplay which will search and display the result.


if (!SearchAndDisplay(strToSearch,words))
{
Response.Write("No matches found.");
}

/*
This function iterates through a given array and finds existence of a string among it.if found returns true and prints the string else returns false.
*/
private bool SearchAndDisplay(string strToSearch,string[]arrContentString)
{
bool isMatchFound = false;
foreach (string tempString in arrContentString)
{
if (System.Text.RegularExpressions.Regex.IsMatch(tempString, strToSearch, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
isMatchFound = true;
Response.Write("Match found in :" + tempString.Trim() + "
");
}

}
return isMatchFound;

}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: