C# Regular Expressions

Regular Expressions in C# helps to describe complex patterns in texts. These C# regular Expressions are commonly known as RegEx in short. The first thing we need to understand is in Regex expression, and everything is essentially a character.

The described C# Regex patterns could be used for searching, extracting, replacing, and modifying text data. C# supports regular expressions by providing classes in System.Text.RegularExpressions namespace.

Regular Expressions in C# are nothing but the patterns. After defining patterns, we can validate by searching, extracting, replacing, modifying.

In general, every structured data have some specific pattern. Let us say for example

www.tutorialgateway.org

Any URL is having some specific pattern like it starts with www and ends with org and in between connected with dots.

[email protected]

Likewise, email id also has a specific pattern like

It starts with some alphanumeric characters, and then there will be ‘@’ symbol after that. Again there may be alphanumeric characters followed by a dot, which is again followed by ‘com’, or ‘org’, etc.

7/12/2018(mm/dd/yyyy)

Date format has a certain pattern like mm-dd-yy format or mm-dd-yyyy format. The C# Regular expressions help us to define such patterns.

Regular expressions look something like the below. The C# Regex for simple email validation is.

^[a-zA-Z0-9]{1,10}@[a-zA-Z0-9]{1,10}.(com|org|in)$

Let us see the basic information to write regular expressions.

B  for Brackets, C  for Carrot, D  for Dollar

BThere are three types of brackets used in regular expression “[“ and curly “{“ braces. Square brackets specify the character which have to be matched while curly brackets specify how many characters. “(“ is for grouping
CCarrot “^” is the start of a regular expression
DDollar “$” the end of the regular expression

C# Regex Examples

The following examples shows some of the simple C# regular expression patterns.

Enter the character that exists between a-g?

[a-g] –

Enter character that exists between a-g with the length of 3?

Regex: ^[a-g]{3}$ – 

Enter character that exists between a-g with max three characters and minimum one character?

Regex: ^[a-g]{1,3}$

Validating data with 7 digits fix numerical format like 8743524, 6864351, etc.

Regex: ^[0-9]{7}$

How to validate numeric data with three as minimum length and six as the maximum length?

^[0-9]{3,7}$

How to validate invoice numbers that have formats like KTG5240, the first three are alphabetical characters, and the remaining is the length of 6 numbers.

^[a-z]{3)[0-9]{6}$

Let us see simple Regex for validating the URL.

Regex: ^www.[A-Za-z0-9]{1,10}.(com|org|in)$

It is always recommended to use verbatim literals instead of regular strings while writing regular expressions in C#.

Verbatim literals start with the special prefix (@) that helps not to interpret backslashes and meta characters in the string.

For instance, instead of writing \\m\\n, we can write it as @” \n\m”, which makes the string readable.

C# Regex String Matching

The C# Regex is a class in the namespace System.Text.RegularExpressions envelop the interfaces to the regular expressions engine, thus allowing us to perform matches and extract data from text using regular expressions.

In C#, there is a static method Regex.Match to test whether the Regex matches a string or not. The enum Regexoptions is an optional setting to the method Regex.Match. The Regex.Match returns a Match object which holds the information of where the match found if there is a match.

The C# Regex string match syntax is

Match match = Regex.Match(InputStr, Pattern, RegexOptions)

Let us see an example to demonstrate the C# Regular expressions. In this C# Regex example, we are demonstrating the regex pattern for “august 25” 

using System;
using System.Text.RegularExpressions;

class program
{
    public static void Main(string[] args)
    {
        string pattern = @"([a-zA-Z]+) (\d+)";
        string input = "special occasion on august 25";
        Match match = Regex.Match(input, pattern);
        if(match.Success)
        {
            Console.WriteLine(match.Value);
        }
        Console.ReadKey();
    }
}

OUTPUT

C# Regex Example 1

ANALYSIS

We have taken a string variable pattern to store the regex pattern. Next, the input is another string variable to store the text in which we want to search the pattern match. And the match is a variable of type Match to store the result that the static method regex.Match returns. If the match is a success, then the match value is printed onto the console.

C# RegEx patterns

The following table shows you the list of common C# RegEx patterns and their descriptions.

abc…Letters
123… Digits
\d Any Digit
\DAny Non-digit character
.Any Character
\.Period
[abc]Only a, b, or c
[^abc] Not a, b, nor c
[a-z]Characters a to z
[0-9]Numbers 0 to 9
\wAny Alphanumeric character
\WAny Non-alphanumeric character
{m}m Repetitions
{m,n}m to n Repetitions
*Zero or more repetitions
+One or more repetitions
?Optional character
\sAny Whitespace
\SAny Non-whitespace character
^…$Starts and ends
(…)Capture Group
(a(bc))Capture Sub-group
(.*)Capture all
(abc|def)Matches abc or def

Let us see another code demonstrating the C# Regex pattern for validating simple email id [email protected]

using System;
using System.Text.RegularExpressions;


class program
{
    public static void Main(string[] args)
    {
        string pattern = "[A-Za-z0-9]{1,20}@[a-zA-Z]{1,20}.(com|org)$";
        string input = "Website is www.tutorialgateway.org, contact us at [email protected]";
        Match match = Regex.Match(input, pattern);
        if(match.Success)
        {
            Console.WriteLine(match.Value);
        }
        Console.ReadKey();
    }
}

OUTPUT

C# Regular expressions Example 2

ANALYSIS

As you have seen in the above example code, the string variable pattern is to store the regex pattern. Variable input is to store the text we give.

Both pattern and input passed as arguments to the Regex.Match method, which returns the match value if the match is successful.