What is Regex ?
Regex is also known as a regular expression in c#. The regular expression is used to provide a pattern which is further used to identify whether the given string or string which we input from the user matches the pattern or not. Regex is a class provided by c#; it does two things,
- Parse the string into regular expression format
- Identify the regular expression pattern from string
For E.g.
string[] str = {"9925612824", "8238783138", "02812451830"};
string strRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9]
{2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)";
Here in the above example, an array of numbers are given, and the regular expression pattern is also given, which will try to find if the numbers match with the pattern. There are some methods in regular expressions which are used to check whether the pattern is matching or not.
Methods
- Public bool IsMatch(string input) :It informs whether the input string matches the pattern or not.
- Public bool IsMatch(string input, int startat) : It specifies the start of the string and finds whether it matches the pattern.
- Public static bool IsMatch(string input, string pattern) : This method checks for only any specified string.
- Public MatchCollection Matches(string input) : It searches for all the occurrences of regular expressions in the string.
- Public string Replace(string input, string replace) : This replaces the string matching the regular expression.
- Public string[] Split(string input) : It splits the string into substrings.