Google News
logo
C# - Interview Questions
What are Regular expressions? Search a string using regular expressions?
Regular expression is a template to match a set of input. The pattern can consist of operators, constructs or character literals. Regex is used for string parsing and replacing the character string.
 
For Example :
 
* matches the preceding character zero or more times. So, a*b regex is equivalent to b, ab, aab, aaab and so on.
 
Searching a string using Regex :
static void Main(string[] args)
{
string[] languages = { "C#", "Python", "Java" };
foreach(string s in languages)
{
if(System.Text.RegularExpressions.Regex.IsMatch(s,"Python"))
{
Console.WriteLine("Match found");
}
}
}
The above example searches for “Python” against the set of inputs from the languages array. It uses Regex.IsMatch which returns true in case if the pattern is found in the input. The pattern can be any regular expression representing the input that we want to match.
Advertisement