Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
.net

What is Regex class in C#?

Tarun Telang

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In C#, we use the Regex class to represent a regular expression. It performs various operations like pattern matching, replacing patterns, and others on strings. It is defined in the System.Text.RegularExpressions namespace.

Syntax

The syntax for the Regex class is as follows:

Regex regexObj = new Regex(pattern); 

Where,

  • regexObj: This is an example of the Regex class.
  • pattern: This is a regular expression pattern.

Constructors

  • Regex(String): This initializes a new instance of the regular expression class that contains the regular expression specified in pattern parameter.
  • Regex(String,RegexOptions): It initializes a new instance of the regular expression class that contains the regular expression pattern specified in the pattern parameter and also specifies an option that modifies the regular expression.
  • Regex(String,RegexOptions,TimeSpan): It initializes a new instance of the regular expression class that contains the regular expression pattern specified in the pattern parameter, specifies an option that modifies the regular expression, and also specifies a time-out interval.

Fields / Properties

  • CacheSize: We use the function to get or set the size of the internal cache array used for storing the compiled regular expression pattern and corresponding regex objects.
  • CultureInvariant: It returns a value that indicates whether the regular expression specified in the Regex constructor is defined as case-insensitive for all cultures or not.
  • ECMAScript: It returns a value that indicates whether the regular expression specified in the Regex constructor is defined as case-insensitive for all cultures or not.
  • IgnoreCase: We use the function to get or set a value that indicates whether the Regex object is defined as case-insensitive.
  • IsMatchTimeout: We use this function to get a value that indicates whether the time-out interval has elapsed.
  • MatchTimeout: We use this function to get or set a time-out interval in milliseconds. If no time-out value is defined, the method uses the default time-out interval.
  • Multiline: It is used to get or set a value that indicates whether the Regex object searches an input string for all matches or only for the first match.
  • RightToLeft: It is used to get or set a value that indicates whether the search for a regular expression pattern is from right to left or from left to right.

Methods

  • Matches(String): It searches an input string for all occurrences of a regular expression and returns information about each occurrence as a collection of match objects.
  • Matches(String,Int32): It searches the specified input string for all occurrences of a regular expression, beginning at the specified starting position in the string, and returns information about each occurrence as a collection of match objects.

Example

The example for using Regex class code in C# is as follows:

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main(string[] args)
    {
         // using the Regex class
        Regex regex = new Regex("abc");

        // checking if a given string matches a regular expression
        Console.WriteLine(regex.IsMatch("abc")); // true
        Console.WriteLine(regex.IsMatch("Abc")); // false

        // getting the first match
        Match match = regex.Match("abcdefabc");
        Console.WriteLine(match); // abc

        // getting all the matches
        MatchCollection matches = regex.Matches("abcdefabc");
        foreach (Match m in matches)
            Console.WriteLine(m); // abc
                                // abc
    }
}
Using Regex class

Explanation

In the above code:

  • Line 12: This code line checks if a given string matches a regular expression.
  • Line 13: It prints True, as it matches.
  • Line 14: It prints False, as it does not match.
  • Line 16: This line gets the first match.
  • Line 17: This line gets all the matches of a given regular expression in a string.

RELATED TAGS

c#
.net

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring