Regular Expressions in C#

01 januari 2021 om 10:00 by ParTech Media - Post a comment

The language C# allows us to perform hundreds of operations while developing an application. One of these operations that the developers love is Pattern Matching. Pattern Matching is quite an important concept that you need to understand while you begin your development journey.

Whenever we search for information over the web, applications use our input text and try to match the text with the resources they find. Ring any bell? Yes, we are talking about a searching engine like Google. These search engines extensively use pattern matching to show us relevant information.

C# typically allows pattern matching using the Regex class. In this article, we are going to understand what is pattern matching, what are regular expressions in C#, the different functions that Regex has to offer, and also the different purposes it serves.

Table of content

  1. What are Regular Expressions in C#?

  2. Uses of Regular Expression

  3. Regex Functions in C#

  4. Advantages of using Regular Expressions

  5. Wrapping up

What are Regular Expressions in C#?

Being an Object-Oriented programming language, C# supports the concepts of inheritance, encapsulation, and polymorphism. It is a highly expressive programming language and allows users to build all sorts of applications for their personal and business needs. C# also allows pattern matching, which helps to enable control flow by inspecting data types and values.

Regular Expressions or Regex is used for pattern matching in C#. Regular Expression is a pattern that is used for checking whether the pattern matches against an input text. In C#, the .NET Framework provides a regular expression engine. A class termed Regex can be found in the System.Text.RegularExpression namespace. The Regex class is used to compile a regular expression. The class also contains various static methods which help to reduce the size of the code since we do not need to explicitly define an object and call the method using the object.

Uses of Regular Expression

Regular Expressions in C# can be used for the following purposes:

  1. Validating a String whether it matches with another String or not.
  2. Replacing text with another string or expression.
  3. Extracting substring from a string.
  4. Validating a form in an application.
  5. Searching for a specific text.
  6. Advanced text manipulation.

Regex Functions in C#

  1. C# regex isMatch – The isMatch() method is used to find whether the given regular expression matches the input string or not. It gives us a Boolean output.

In the above code, we first import the Regular Expressions namespace and then create a program to check whether the given input strings match the Regular Expression “row”.

The above code yields an output and shows us that the words Prowl, Growl, row, and Grown match the regular expression whereas fun does not match.

  1. C# Regex Match index – It indicates whether a match is successful or not by returning a Boolean value. The NextMatch() function then finds the results for the next match and returns a new Match object.

In the above code, we look for all the matches for the word “Africa”. We also add the (n) to include African as well in the regular expression. The code yields an output and returns all the occurrences of the word “Africa” and the index at which the word is present.

  1. C# Regex Matches – The matches function searches an input text for all occurrences of a regular expression and returns all the matches.

The above code finds all the HTML tags present in the input string. It returns all the Match objects in a collection. If no matches are found, it returns an empty collection.

  1. C# Regex Replace – The Replace() method is used to replace strings and allow modification of input text.

The above code replaces all the extra white spaces and gives a single white space. So, the new resulting string is “African Wildlife”.

  1. C# Regex Split – The split() method is used to split the input string based on a regular expression or a pattern.

The above code splits the input string using the regex pattern \D+ and loops through the numbers array and prints the values.

The output shows as:




Advantages of using Regular Expressions

  • The breadth of using Regular Expression is very extensive. One regular expression can be used to validate any kind of input.
  • Write less code and keep clean code by doing more work with less code.
  • It powers users to do faster validations and helps to avoid using IF-ELSE commands.
  • Regular Expressions are supported widely by most programming languages.

Wrapping Up

Regular Expressions are one of the most useful features available for C# programmers. It is often used by developers to validate a form or simply search for a string in an application. In this post, we have seen many such applications of regular expressions and pattern matching. We have also seen some practical examples of how to use it effectively in your next program.