Javatpoint Logo
Javatpoint Logo

Regular Expression in C#

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.

Regular Expression

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

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'.

Here we have a code which contain the long text having the author name which we have to be parsed.

Here we will use Matches method to find all the matches which returns the MatchCollection.

Now let us take an example to find the letter 'M'.

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.

Regular Expression Example in C#

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.

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:

Regular Expression in C#

Regex Class method in C#

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.

Regex Replace String Example in C#

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#.

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:

Regular Expression in C#

Find the Duplicate Words in Regex C#

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#.

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:

Regular Expression in C#
Next TopicC# Tutorial




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA