Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Replace parts of a string with C# Regex
Regular expressions (Regex) in C# provide powerful pattern matching and string replacement capabilities. The Regex.Replace() method allows you to replace parts of a string that match a specified pattern with a replacement string.
Syntax
Following is the syntax for using Regex.Replace() method −
Regex.Replace(input, pattern, replacement);
Parameters
- input − The string to search for a match
- pattern − The regular expression pattern to match
- replacement − The replacement string
Return Value
Returns a new string where all matches of the pattern are replaced with the replacement string. If no matches are found, the original string is returned unchanged.
Using Basic Pattern Matching
The dot (.) in regex matches any single character. In the pattern B.t, it matches any three-character sequence starting with 'B' and ending with 't' −
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string str = "Bit and Bat";
Console.WriteLine("Original: " + str);
string result = Regex.Replace(str, "B.t", "BAT");
Console.WriteLine("Result: " + result);
}
}
The output of the above code is −
Original: Bit and Bat Result: BAT and BAT
Using Character Classes
You can use character classes to match specific sets of characters. For example, [aeiou] matches any vowel −
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string text = "The quick brown fox";
Console.WriteLine("Original: " + text);
string result = Regex.Replace(text, "[aeiou]", "*");
Console.WriteLine("Vowels replaced: " + result);
}
}
The output of the above code is −
Original: The quick brown fox Vowels replaced: Th* q**ck br*wn f*x
Using Capture Groups
Capture groups allow you to reference parts of the matched pattern in the replacement string using $1, $2, etc. −
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string phoneNumber = "123-456-7890";
Console.WriteLine("Original: " + phoneNumber);
string formatted = Regex.Replace(phoneNumber, @"(\d{3})-(\d{3})-(\d{4})", "($1) $2-$3");
Console.WriteLine("Formatted: " + formatted);
}
}
The output of the above code is −
Original: 123-456-7890 Formatted: (123) 456-7890
Using RegexOptions
You can specify additional options like case-insensitive matching using RegexOptions −
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string text = "Hello WORLD hello world";
Console.WriteLine("Original: " + text);
string result = Regex.Replace(text, "hello", "Hi", RegexOptions.IgnoreCase);
Console.WriteLine("Case-insensitive replace: " + result);
}
}
The output of the above code is −
Original: Hello WORLD hello world Case-insensitive replace: Hi WORLD Hi world
Common Regex Patterns
| Pattern | Description | Example |
|---|---|---|
\d |
Any digit (0-9) | Matches "5" in "abc5def" |
\w |
Any word character | Matches letters, digits, underscore |
\s |
Any whitespace character | Matches spaces, tabs, newlines |
+ |
One or more occurrences |
\d+ matches "123" |
* |
Zero or more occurrences |
a* matches "", "a", "aa" |
Conclusion
The Regex.Replace() method in C# is a powerful tool for pattern-based string replacement. It supports complex patterns, capture groups, and various options to handle different replacement scenarios efficiently. Use it when you need more sophisticated string manipulation than simple String.Replace() can provide.
