C# - Regular Expressions

Regular expressions (regex or regexp) in C# are a powerful tool for pattern matching and string manipulation. They provide a concise and flexible way to search, match, and manipulate strings based on specified patterns. In C#, regular expressions are part of the System.Text.RegularExpressions namespace.

Here's a basic overview of using regular expressions in C#:

Creating a Regex Object:

To use regular expressions in C#, you typically create a Regex object with the pattern you want to match.

using System;
using System.Text.RegularExpressions;

class Program
    static void Main()
        string pattern = "/\d+"; // Match one or more digits
        Regex regex = new Regex(pattern);

        // Use the regex object for matching
        string input = "1234";
        bool isMatch = regex.IsMatch(input);

        Console.WriteLine($"Is it a match? {isMatch}");

Common Regex Methods:

  • IsMatch: Checks if the input string contains a match for the pattern.
  • Match: Returns the first occurrence of the pattern within the input string.
  • Matches: Returns all occurrences of the pattern within the input string.
  • Replace: Replaces occurrences of the pattern with a specified replacement string.
  • Split: Splits the input string into an array of substrings based on the pattern.

Regex Patterns:

Regular expressions consist of various characters and symbols that define patterns.

For example:

  • \d: Matches any digit.
  • \w: Matches any word character (alphanumeric + underscore).
  • .: Matches any character except a newline.
  • *, +, ?: Quantifiers for matching zero or more, one or more, or zero or one occurrences, respectively.
  • ^, $: Anchors for matching the start or end of a line.

Groups and Capturing:

You can use parentheses to create capturing groups, which allow you to extract specific parts of the matched text.

string pattern = "(d{2})(d{2})(d{4})"; // Matches dates in the format DD/MM/YYYY
Regex regex = new Regex(pattern);

string input = "25/12/2022";
Match match = regex.Match(input);

if (match.Success)
    Console.WriteLine($"Day: {match.Groups[1].Value}, Month: {match.Groups[2].Value}, Year: {match.Groups[3].Value}");

Options and Modifiers:

Regular expressions support various options and modifiers, such as case-insensitive matching (RegexOptions.IgnoreCase) or multiline mode (RegexOptions.Multiline).

string pattern = "example";
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);

string input = "Example";
bool isMatch = regex.IsMatch(input);

Console.WriteLine($"Is it a match? {isMatch}");

Regular expressions can become quite complex, and their full syntax is beyond the scope of a brief overview. However, they are a powerful tool for advanced string manipulation and pattern matching in C#.