Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Regular Expressions

Introduction to Regular Expressions

Regular expressions (regex) are patterns used to match character combinations in strings. In .NET, regular expressions are used for searching, matching, and replacing text.

Basic Syntax

The System.Text.RegularExpressions namespace provides the Regex class for working with regular expressions in .NET. Basic syntax includes:

  • Literal characters: a, 1, etc.
  • Character classes: \d (digits), \w (word characters), \s (whitespace).
  • Quantifiers: * (0 or more), + (1 or more), ? (0 or 1), {n} (exactly n times).
  • Anchors: ^ (start of string), $ (end of string).

Creating a Regex Object

You can create a Regex object by passing a pattern to the constructor:

Example: Creating a Regex Object


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"\d+");
        string input = "There are 123 apples";
        Match match = regex.Match(input);
        if (match.Success) {
            Console.WriteLine($"Found a match: {match.Value}");
        }
    }
}
    

Common Operations

Some common operations with regular expressions include matching, searching, and replacing text.

Matching

Use the IsMatch method to determine if the input string matches the pattern.

Example: Matching


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"^\d+$");
        string input = "12345";
        bool isMatch = regex.IsMatch(input);
        Console.WriteLine($"Is match: {isMatch}");
    }
}
    

Searching

Use the Match or Matches method to search for patterns in the input string.

Example: Searching


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"\d+");
        string input = "The price is 100 dollars and 20 cents.";
        MatchCollection matches = regex.Matches(input);
        foreach (Match match in matches) {
            Console.WriteLine($"Found match: {match.Value}");
        }
    }
}
    

Replacing

Use the Replace method to replace matched patterns in the input string.

Example: Replacing


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"\d+");
        string input = "Replace 123 and 456 with numbers.";
        string result = regex.Replace(input, "#");
        Console.WriteLine(result);
    }
}
    

Advanced Topics

Regular expressions can also handle more advanced operations such as capturing groups, lookaheads, and lookbehinds.

Capturing Groups

Use parentheses to define capturing groups and the Groups property to access them.

Example: Capturing Groups


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"(\d{3})-(\d{2})-(\d{4})");
        string input = "My number is 123-45-6789.";
        Match match = regex.Match(input);
        if (match.Success) {
            Console.WriteLine($"Full match: {match.Value}");
            Console.WriteLine($"Group 1: {match.Groups[1].Value}");
            Console.WriteLine($"Group 2: {match.Groups[2].Value}");
            Console.WriteLine($"Group 3: {match.Groups[3].Value}");
        }
    }
}
    

Lookaheads and Lookbehinds

Lookaheads and lookbehinds are zero-width assertions that match a group before or after the main pattern without including it in the result.

Example: Lookahead


using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        Regex regex = new Regex(@"\d+(?= dollars)");
        string input = "The price is 100 dollars.";
        Match match = regex.Match(input);
        if (match.Success) {
            Console.WriteLine($"Found match: {match.Value}");
        }
    }
}
    

Conclusion

Regular expressions are a powerful tool for pattern matching and text manipulation in .NET. By understanding the syntax and common operations, you can efficiently perform complex string processing tasks.