In C #
programming, regular expression is used to validate user input and to validate
the starting of words, we use caret (^) and for ending words, we use dollar ($).
In C#, The
Regex class is used to verify the specific character pattern and it offers
several methods and properties to parse input string and verify the specific
patterns.
Match the beginning of the string:
Caret (^) is
used to matches the beginning of the string
string regExPattern = "^t";
Regex regex = new Regex(regExPattern);
Console.WriteLine(regex.IsMatch("tom"));
// true
Console.WriteLine(regex.IsMatch("dom"));
// false
Console.WriteLine(regex.IsMatch("jon"));
// false
Match the ending of the string:
Dollar ($) is
used to matches the ending of the string
string regExPattern = @".com$";
Regex regex = new Regex(regExPattern);
Console.WriteLine(regex.IsMatch("tom@mail.com"));
// true
Console.WriteLine(regex.IsMatch("dom"));
// false
Console.WriteLine(regex.IsMatch("jom@mail.com"));
// true
No comments:
Post a Comment