In C#, Regular Expression is used for the parsing and validation of the given text to match with the defined pattern (for example, an email address). The pattern can contain operators, characters literals, or constructs.
To process the text with the regular Expression in .NET Framework, generally, we used the regular expression engine. In C#, Regular Expression is shown by the Regex.
We used the Regular Expression to check whether the given string matches the pattern or not. Regular Expression or Regex is a sequence of characters that defines the pattern. The pattern can contain numbers, literals, operators, characters, etc. We used the Patterns to search the strings or files. Here we will see if the matches are found or not.
Generally, the Regular Expressions are used for the parsing, finding the strings or validations, etc.
Here we are taking an example where we can use Regular Expression for checking the social security number, valid date of birth, for matching the full name where the first and last name are separated by the comma, for replacing the substring, for the correct email format, currency format and so on.
Regex class shows the regular expression engine in the .NET Framework. Regex class is used to parse a large amount of text for finding the specific character pattern. We can use the Regex class for the extraction, editing, for the replacement, or to delete the text of the substring.
System.Text.RegularExpressions namespace contains the Regex class. Regex Class has the string pattern as a parameter with the other optional parameter.
Now we will create a pattern from the regex. In this code we have to match the pattern with the word which is starting with the char 'M'.
// Create a pattern for the word that starts with letter "M" string pattern = @"\b[M]\w+"; // Create a Regex Regex rgex = new Regex(pattern);
Here we have a code which contain the long text having the author name which we have to be parsed.
// Long string string authors = "Ramesh chand, Rakeshwar";
Here we will use Matches method to find all the matches which returns the MatchCollection.
// Get all matches MatchCollection matchedAuthors = rg.Matches(authors); To find the matches collection we will use the For loop // Print all matched authors for (int count = 0; count < matchedAuthors.Count; count++) Console.WriteLine(matchedAuthors[count]. Value);
Now let us take an example to find the letter 'M'.
// Create a pattern for a word which starts with letter "M" string pattern1 = @"\b[M]\w+"; // Create a Regex Regex rg = new Regex(pattern1); // Long string string Authors = "Ramesh Chand,Rakeshwar"; // Get all matches MatchCollection matchedAuthors = rg.Matches(Authors ); // For print all matched authors for (int count = 0; count < matchedAuthors.Count; count++) Console.WriteLine(matchedAuthors[count].Value);
From the above example we tried to find the char 'M'. But here a situation is arising if the word will start with small 'm'. In this case, we will use RegexOption.IgnoreCase parameter so that the Regex will ignore the uppercase or lowercase.
// Create a pattern for a word that starts with letter "M" string pattern1 = @"\b[m]\w+"; // Create a Regex Regex rgex = new Regex(pattern, RegexOptions.IgnoreCase);
Here we are taking an example to validate the text whether the email is in proper format or not. For this we will use Regex class.
using System; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); string email = "support@rookienerd.com"; var result = Regex.IsMatch(email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"); Console.Write("Is valid: {0} ", result); Console.ReadLine(); } } }
From the above example, we are validating the input string, which is in a valid format or not. For this, we are using the Regex class. For the validation of the input text, we used the IsMatch method and the pattern of Regular Expression.
After the execution of the above code, we get the result, as shown below:
To perform the various operations on the input string, the Regex class contains different methods. The table is having the list of various methods of Regex in C#.
Method | Description |
---|---|
IsMatch | We used the IsMatch method for finding out whether the given input string matches the regular expression pattern or not. |
Matches | Matches method is used to return the text, which matches with the regular expression pattern. |
Replace | Replace method is used to replace the text, which matches the regular expression pattern. |
Split | We used the Split method for splitting the string into the array of the substring at those positions, which matches with the regular expression pattern. |
The above method of Regex class is used for the validation, replacement, or splitting the values of the string with the regular expression pattern, which is based on the requirements.
With the help of this example, we are trying to find the substring by using the regular expression pattern, which replaces the required values in C#.
using System; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string str = "Hi,welcome@rookienerd.com"; string result = Regex.Replace(str, "[^a-zA-Z0-9_]+", " "); Console.Write("{0} ", result); Console.ReadLine(); } } }
In the above example, we used the Regex. Replace method is used for finding and replacement of the special characters in a string with the space by using the regular expression pattern ("[^a-zA-Z0-9_]+").
In the above example, the pattern of the Regular Expression ("[^a-zA-Z0-9_]+") tried to match with the single character, which is not defined in the group of the character.
After executing the above program, we will get the following output, as shown below:
By using the regular expression pattern, we can easily find out the duplicate words.
In this example, we are trying to find the duplicate word in the given string by using the Regex class method in C#.
using System; using System.Text.RegularExpressions; namespace ConsoleApp1 { class Program1 { static void Main(string[] args) { string str1 = "Welcome To to rookienerd. Here we can learn C# easily easily way"; MatchCollection collection = Regex.Matches(str1, @"\b(\w+?)\s\1\b", RegexOptions.IgnoreCase); foreach (Match m in collection) { Console.WriteLine("{0} (duplicates '{1}') at position {2}", m.Value, m.Groups[1].Value, m.Index); } Console.ReadLine(); } } }
The above example used the Regex. Matches method is used to find out the duplicate words by using the regular expression pattern ("\b(\w+?)\s\1\b").
After the execution of the above code, we will get the following output, as shown below: