Regular Expressions in C#

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

C# Programming Guide

C# is a powerful and highly expressive programming language that allows programmers to create business applications, as well as, personal needs software. C# offers lots of useful features for developers and pattern matching is among them. The .NET language allows programmers to use pattern matching mechanisms that help the control flow of the application by inspecting values and data types.

C# uses regular expressions – or Regex – for pattern matching. Regular expressions are used for checking whether a pattern matches against an input value or not. The .NET framework provides a regular expression engine so that developers can bring the power of regular expressions directly into your program without much effort.

C# provides a namespace called ‘System.Text.RegularExpressions’, which contains a class named ‘Regex’ that you can make use of when compiling a regular expression in your program.

Read: Working with Strings in C#

What Are Regular Expressions Used For?

Regular expressions serve a variety of functions in programs. Below is a list of just a few of the use cases C# developers might use Regex for:

  • Searching for a specific text in a string
  • Checking whether a string matches another string or text
  • Validating a form in any application
  • Replacing a text with another text or expression
  • Extracting a particular text from a string
  • Advanced level text manipulations

What are the Advantages of Regular Expressions?

Regular expressions and Regex offer a number of advantages for C# and .NET developers, including:

  • One regular expression is enough to validate any kind of user input
  • Faster validation of data.
  • Developers do not need to use if-else statements while validating user data.
  • More efficient and cleaner code with fewer lines of code required for data validation.
  • Widely supported by programming languages.

Regular Expression Functions

In this section, we will discuss some frequently used functions of the Regex class which are frequently used by C# programmers. The Regex class is found in the namespace System.Text.RegularExpressions and is shipped with all versions of .NET. So if you have .NET installed on your computer, you are ready to make use of it.

The Regex class has static, as well as, instance methods. Instance methods are those that require creating a Regex object first before calling them. For illustration, both of the versions are used in this section: I have used the static version of methods in the IsMatch() and Split() is used and for the Match() and Replace() methods, the instance version is used.

Read: Tips for Writing Clean Code in C#

The IsMatch() Method in C#

IsMatch() is one of the basic methods of the Regex class that is used to find whether the given regular expression matches the input string or not. If there is a match found, it returns true, otherwise, it would return false.

var mytext = "example";
var result = Regex.IsMatch(mytext, "[a-z]");

In the above example, the IsMatch() method checks whether the variable mytext contains a lowercase letter or not. The variable result will be evaluated to true because the Regex pattern [a-z] will match the first lowercase letter in example: e.

The Match() Method in C#

The Match() method returns a Boolean (true or false) value based on whether a match is successful or not. The Match() method returns a Match object and this object contains info such as the matched segment of the text (via the Value property) and the starting index of the matched text segment (via Index property).

Here is an example that depicts the use of Match() in C#:

        Regex reg = new Regex(@"a+b");
        Match match = reg.Match("bcaaacd");
        if (match.Success)
            Console.WriteLine("Matched Value: " + match.Value);

Running this code in your code editor or integrated development environment will produce the following output:

Matched Value: baaa

In this example, the Match() method uses the regular expression a+b to match the occurrence of letters ‘a’ and ‘b’ in the text ‘bcaaacd’. Note that the Match() method returns the Match object and by using that object, we can print the matched text segment via the Value property of the Match object.

Read: Search and Replace with Regular Expressions

The Replace() Method in C#

The Replace() method implicitly matches the text using the Match() method and then replaces the matched text with the replacement string. This method returns a copy of the modified text:

   string inputText = “Wish I am  on  Everest”;
   string pattern=”\\s+”;
   string replacedText=” “;
   Regex reg=new Regex(pattern);
   string result=reg.Replace(inputText, replacedText);
   Console.WriteLine(“Original Text:”, inputText);
   Console.WriteLine(“Replaced Text:”, replacedText);

This code snippet replaces all the extra white spaces with a single white space. So, the output of the code snippet above looks like the following:

Original Text:Wish I am  on  Everest
Replaced Text:Wish I am on Everest

The Split() Method in C#

The Split() method splits the input text based on a given regular expression or pattern. For example, the following code snippet shows how the Split() method works in C#:

string url = "C:/TestApplication/1";  
string[] splitUrl = Regex.Split(url, @"/");  
foreach (var i in splitUrl)  

This results in the following output:


In the above code example, we have used the Split() method, which splits the given string based on the recurring pattern. Here, in the example, we have provided a URL string: C:/TestApplication/1

Let’s say we want to replace the ‘/’ from the given input and get all the substrings. For this, we have given the pattern that we want to use as a splitting point. The Split() method returns an array and we can run a loop through each item of the array to retrieve all of the substrings (this is beyond the scope of this article, but is an example of how the Split() method can be used).

Read: Working with Arrays in C#

Conclusion to Regex in C# and .NET

Regular Expressions are one of the most useful features of the C# language. Programmers can use Regex for validating a form or searching a specific text in a string. I hope you enjoyed learning the practical examples of pattern matching and regular expressions and learned how you can use the Regex class – and its methods – effectively in a C# application.

Tariq Siddiqui
Tariq Siddiqui
A graduate in MS Computer Applications and a Web Developer from India with diverse skills across multiple Web development technologies. Enjoys writing about any tech topic, including programming, algorithms and cloud computing. Traveling and playing video games are the hobbies that interest me most.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read