Using regular expressions

C#:
Using regular expressions

How to:

Simple Pattern Matching

To check if a string contains a specific pattern, you can use the Regex.IsMatch method from the System.Text.RegularExpressions namespace.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string sampleText = "Hello, World!";
        string pattern = "World";
        bool containsPattern = Regex.IsMatch(sampleText, pattern);

        Console.WriteLine(containsPattern);  // Output: True
    }
}

Extracting Data

Extracting data from a string using groups in a regex can be done with the Regex.Match method.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string sampleText = "Date: 2023-04-12";
        string pattern = @"Date: (\d{4})-(\d{2})-(\d{2})";
        Match match = Regex.Match(sampleText, pattern);

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

Replacing Text

The Regex.Replace method lets you replace text in a string that matches a specified pattern.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string sampleText = "Visit Microsoft!";
        string pattern = "Microsoft";
        string replacement = "Google";

        string result = Regex.Replace(sampleText, pattern, replacement);

        Console.WriteLine(result);  // Output: Visit Google!
    }
}

Splitting Strings

You can split a string into an array based on a regex pattern using the Regex.Split method.

using System;
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        string sampleText = "one,two,three,four,five";
        string pattern = ",";

        string[] result = Regex.Split(sampleText, pattern);

        foreach (string item in result)
        {
            Console.WriteLine(item);
        }
        // Output: 
        // one
        // two
        // three
        // four
        // five
    }
}

Using Third-Party Libraries

While the .NET Framework provides extensive support for regular expressions, there are also third-party libraries such as PCRE.NET that offer Perl-compatible regular expressions (PCRE) in C#. This can be useful if you need features or syntax from Perl’s regex engine that are not available in .NET’s implementation.

To use PCRE.NET, you would first install its NuGet package, and then you can use it similarly to how you use the native .NET regex classes.

// Example using PCRE.NET here
// Note: Imagine a sample similar to the ones above, tailored to showcase a feature unique to PCRE.NET.

When integrating third-party libraries for regular expressions, always consult their documentation for detailed usage and compatibility information.