C# - Regular Expressions



A regular expression is a pattern that could be matched against an input text. The .Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs.

Constructs for Defining Regular Expressions

There are various categories of characters, operators, and constructs that lets you to define regular expressions. Click the following links to find these constructs.

The Regex Class

The Regex class is used for representing a regular expression. It has the following commonly used methods −

Sr.No. Methods & Description
1

public bool IsMatch(string input)

Indicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.

2

public bool IsMatch(string input, int startat)

Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.

3

public static bool IsMatch(string input, string pattern)

Indicates whether the specified regular expression finds a match in the specified input string.

4

public MatchCollection Matches(string input)

Searches the specified input string for all occurrences of a regular expression.

5

public string Replace(string input, string replacement)

In a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.

6

public string[] Split(string input)

Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.

For the complete list of methods and properties, please read the Microsoft documentation on C#.

Use of Regular Expressions

The following are some uses of regular expressions:

  • Pattern Matching – Regular expressions are used to find text patterns in strings.
  • Validation – Regular expressions are used for different types of validations such as validating email, phone numbers, etc.
  • Replacement – Regular expressions can also be used to replace specific patterns in text.
  • Extraction – You can also use regular expressions to extract specific parts of a string based on your requirements.

Required Namespaces for Regular Expressions (RegEx)

To work with Regex in C#, you need to include:

using System;
using System.Text.RegularExpressions;

Basic Regex Syntax

Here are the syntaxes of some essential Regex elements:

Pattern Description Example
. Any single character "h.t" matches "hat", "hit"
\d Any digit (0-9) "abc123" = Matches 123
\w Any word character (a-z, A-Z, 0-9, _) "hello_world"
\s Any whitespace "hello world"
\b Word boundary "\bcat\b" matches "cat", not "category"
^ Start of string "^Hello" matches "Hello" at start
$ End of string "World$" matches "World" at end
* Zero or more occurrences "A*" matches "A", "AA", ""
+ One or more occurrences "A+" matches "A", "AA", not ""
{n,m} Between n and m occurrences "\d{2,4}" matches 12, 123, 1234

String Pattern Matching Using RegEx

You can check whether a string matches a specific pattern by using the Regex.Matches() function.

Example

The following example matches words that start with 'S' −

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);
         
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "A Thousand Splendid Suns";
         
         Console.WriteLine("Matching words that start with 'S': ");
         showMatch(str, @"\bS\S*");
         Console.ReadKey();
      }
   }
}

The following example demonstrates matching words that start with 'S' in the string −

Matching words that start with 'S':
The Expression: \bS\S*
Splendid
Suns

Example

The following example matches words that start with 'm' and end with 'e' −

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      private static void showMatch(string text, string expr) {
         Console.WriteLine("The Expression: " + expr);
         MatchCollection mc = Regex.Matches(text, expr);
         
         foreach (Match m in mc) {
            Console.WriteLine(m);
         }
      }
      static void Main(string[] args) {
         string str = "make maze and manage to measure it";

         Console.WriteLine("Matching words start with 'm' and ends with 'e':");
         showMatch(str, @"\bm\S*e\b");
         Console.ReadKey();
      }
   }
}

The following example demonstrates matching words that start with 'm' and end with 'e' −

Matching words start with 'm' and ends with 'e':
The Expression: \bm\S*e\b
make
maze
manage
measure

String Replacement Using RegEx

You can replace multiple spaces with a single space in a string using the Regex.Replace() function.

Example

The following example replaces extra white spaces in a string with a single space −

using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string input = "Hello   World   ";
         string pattern = "\\s+";
         string replacement = " ";
         
         Regex rgx = new Regex(pattern);
         string result = rgx.Replace(input, replacement);

         Console.WriteLine("Original String: {0}", input);
         Console.WriteLine("Replacement String: {0}", result);    
         Console.ReadKey();
      }
   }
}

The following example demonstrates replacing multiple spaces with a single space −

Original String: Hello   World   
Replacement String: Hello World   

Email Validating Using RegEx

You can validate an email address using a regular expression that checks for proper format (e.g., [email protected]).

Example

The following example demonstrates how to validate an email address using regex −

using System;
using System.Text.RegularExpressions;

namespace RegExApplication {
   class Program {
      static void Main(string[] args) {
         string email = "[email protected]";
         string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";

         Regex rgx = new Regex(pattern);
         bool isValid = rgx.IsMatch(email);

         if (isValid) {
            Console.WriteLine("The email address is valid.");
         } else {
            Console.WriteLine("The email address is invalid.");
         }

         Console.ReadKey();
      }
   }
}

The following example demonstrates how to validate an email address using regex −

The email address is valid.
Advertisements