Home > Article > Backend Development > Regular Expression in C#
Pattern matching is done in C# using regular expression and regex class of C# is used for creating regular expression in C#, a standard for pattern matching in strings and for replacement is set using regular expression and it tells the computer through the user how to look for a specific pattern in a string and what must be the response when it finds the specific pattern it is looking for and regex is the abbreviation for a regular expression, overall regular expressions in C# is a powerful method to identify and replace the text in the strings that are defined in a particular format.
The following is a list of the basic syntax used for regular expressions in C#. They are:
The list of important quantifiers are as follows:
The list of important special characters are as follows:
The characters can be grouped by putting them between square brackets. By doing this, at least one character in the input will be a match with any character in the class.
[]: A range of characters can be matched using []. Consider the example [Xyz]. This expression matches any of x, y, and z.
Consider the example [c-r]. This expression matches any of the characters between c and r.
The things can be groups together using the parenthesis ( and ).
Basically, there are two types of regular expression engines. They are text-directed engines and regex-directed engine. A regex-directed engine scans through the regex expression trying to match the next token in the regex expression to the next character. The regex advances if a match is found, otherwise it goes back to the previous position in the regex and the string to be parsed where it can try different paths through the regex expression. A text-directed engine scans through the string trying all the permutations of the regex expression before moving to the next character in the string There is no backtracking or going backward in-text directed engine. The leftmost match is always returned by the regex engine even if there are possibilities of finding the exact matches later. The engine begins with the first character of the string whenever a regex is to be applied to the string. All the possible permutations are applied at the first character and the results seem to fail, then the permutations are moved to the second character in the string and this process goes on until the regex engine finds the exact match.
Consider the example Check the water in the bathtub before going to the bath. The regex engine is asked to find the word bath from the above sentence. The first character C is matched with b by the regex engine and this is a failure. So, the next character H tries to match with b by the regex engine and again this is a failure. This goes on and when the regex engine tries to match the 24th character with b, it matches. So, it goes on and matches the word bath from the bathtub with word bath and the engine reports the word bath from the bathtub as a correct match and it will not go on further in the statement to see if there are any other matches. This is how the regex engine works internally.
The regular expression in C# makes use of the following methods. They are:
C# program to demonstrate the use of regular expressions for the verification of mobile numbers.
Code:
using System; using System.Text.RegularExpressions; class Check { static void Main(string[] args) { //Mobile numbers are given as a input to an array of strings string[] nos = {"9902147368", "9611967273", "63661820954"}; foreach(string s in nos) { Console.WriteLine("The mobile number {0} {1} a valid number.", s, checkvalid(s) ? "is" : "is not"); } Console.ReadKey(); } // Regex expressions are verified through this code block public static bool checkvalid(string Number) { string cRegex = @"(^[0-9]{10}$)|(^\+[0-9]{2}\s+[0-9] {2}[0-9]{8}$)|(^[0-9]{3}-[0-9]{4}-[0-9]{4}$)"; Regex res = new Regex(cRegex); if (res.IsMatch(Number)) return (true); else return (false); } }
Output:
The above is the detailed content of Regular Expression in C#. For more information, please follow other related articles on the PHP Chinese website!