C# Regex Examples
Regular expressions (regex or regexp) in C# are powerful tools for pattern matching and text manipulation. They allow you to define search patterns that can match and extract specific substrings within a larger text.
Using the Regex Class in C#
C# provides the System.Text.RegularExpressions.Regex class to work with regular expressions. You need to include the using System.Text.RegularExpressions; namespace.
Here is an example of a regular expression in C#:
This regular expression matches any string that contains one or more alphanumeric characters.
Search for a match in a string
Here is an example of how to use the regular expression to search for a match in a string:
Simple Pattern Matching
You can use regex to find simple patterns in a string. For example, let's find all email addresses in a given text:
In this example, the pattern \b\w+@\w+\.\w+\b matches words that look like email addresses.
- \b indicates word boundaries
- \w+ matches one or more word characters
- @ matches the literal "@" symbol
- \. matches the dot in the domain
The Matches method finds all matches in the text and stores them in a MatchCollection.
Capturing Groups
You can use capturing groups to extract specific parts of a match. Let's extract the username and domain from email addresses:
In this example, pattern @"(\w+)@(\w+\.\w+)\b"; capturing groups for username and domain
- (\w+) captures the username
- (\w+\.\w+) captures the domain
Replace with Regex
You can also use regex to perform string replacements based on patterns. For example, let's replace all occurrences of "apple" with "orange" in a text:
In the above example, we use the Replace method to substitute all occurrences of "apple" with "orange" in the text.
Validation with Regex
Regular expressions are commonly used for input validation. For instance, you can validate that a string follows a specific format, like checking if a string is a valid date:
Here, IsMatch checks if the date string matches the specified pattern for a valid date.
Extract data from a string
Regular expressions can also be used to extract data from a string
Conclusion
Regular expressions in C# (regex) are powerful tools for pattern matching and text manipulation. They enable you to define and search for specific patterns within strings, making them invaluable for tasks like validation, data extraction, and text processing. By utilizing the System.Text.RegularExpressions namespace, developers can create versatile regex patterns and use them effectively in C# applications.
- Asynchronous programming in C#
- Singleton Class in C#
- Using The CQRS Pattern In C#
- 3-Tier Architecture in C#
- Lambda Expressions in C#
- Binary Search using C#
- Abstract Class In C#
- Constructors and Its Types in C#
- How to serialize and deserialize JSON in C#
- Global using Directive in C# 10
- Recursion in C#
- C# String Concatenation
- DES encryption/decryption in C#
- Triple DES Encryption and Decryption in C#
- Encrypt and Decrypt data using RSA algorithm in C#