Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Regular Expressions in C#

Introduction

Regular expressions (regex) are powerful tools for pattern matching and text manipulation. They are used to search, edit, or manipulate text and data. In C#, regular expressions are implemented through the System.Text.RegularExpressions namespace.

Basic Syntax

A regular expression is a sequence of characters that forms a search pattern. This pattern can be used to match strings or substrings within text. Here are some basic elements of regex:

  • . - Matches any single character except newline.
  • ^ - Matches the start of the string.
  • $ - Matches the end of the string.
  • * - Matches 0 or more repetitions of the preceding element.
  • + - Matches 1 or more repetitions of the preceding element.
  • ? - Matches 0 or 1 repetition of the preceding element.
  • [abc] - Matches any single character within the brackets.
  • [^abc] - Matches any single character not within the brackets.
  • (abc) - Grouping: Matches the sequence "abc".

Using Regex in C#

To use regular expressions in C#, you need to include the System.Text.RegularExpressions namespace. The Regex class is the main class for handling regex operations.

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string input = "Hello, World!";
        string pattern = @"\bWorld\b";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        
        if (match.Success) {
            Console.WriteLine("Match found: " + match.Value);
        } else {
            Console.WriteLine("No match found.");
        }
    }
}
                

Common Tasks

1. Validating Input

Regular expressions can be used to validate input formats, such as email addresses or phone numbers. For instance, the following regex pattern can be used to validate an email address:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string input = "user@example.com";
        string pattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
        Regex regex = new Regex(pattern);
        bool isValid = regex.IsMatch(input);
        
        if (isValid) {
            Console.WriteLine("Valid email address.");
        } else {
            Console.WriteLine("Invalid email address.");
        }
    }
}
                

2. Searching and Extracting

Regex can be used to search for patterns and extract substrings from text. For example, extracting all numbers from a string:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string input = "My phone number is 123-456-7890.";
        string pattern = @"\d+";
        Regex regex = new Regex(pattern);
        MatchCollection matches = regex.Matches(input);
        
        foreach (Match match in matches) {
            Console.WriteLine("Found number: " + match.Value);
        }
    }
}
                

Advanced Features

1. Lookaheads and Lookbehinds

Lookaheads and lookbehinds are zero-width assertions that allow you to match a pattern only if it is (or is not) followed or preceded by another pattern. For example, matching "foo" only if it is followed by "bar":

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string input = "foobar";
        string pattern = @"foo(?=bar)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        
        if (match.Success) {
            Console.WriteLine("Match found: " + match.Value);
        } else {
            Console.WriteLine("No match found.");
        }
    }
}
                

2. Non-Capturing Groups

Non-capturing groups allow you to group parts of a regex without capturing them for back-references. Use (?:...) for non-capturing groups:

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string input = "foobar";
        string pattern = @"(?:foo)(bar)";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(input);
        
        if (match.Success) {
            Console.WriteLine("Whole match: " + match.Value);
            Console.WriteLine("Captured group: " + match.Groups[1].Value);
        } else {
            Console.WriteLine("No match found.");
        }
    }
}
                

Conclusion

Regular expressions are a powerful tool for text manipulation and data validation. By mastering regex, you can efficiently search, extract, and manipulate text in your C# applications. Experiment with different patterns and get comfortable with the syntax to make the most out of this invaluable tool.