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
How to find a matching substring using regular expression in C#?
Regular expressions in C# provide a powerful way to search for specific patterns within strings. The Regex.Matches() method from the System.Text.RegularExpressions namespace allows you to find all occurrences of a pattern in a string.
To find a matching substring, you create a regex pattern and use it to search through your target string. The pattern can be a simple literal match or include special regex metacharacters for more complex searches.
Syntax
Following is the basic syntax for finding matches using regular expressions −
MatchCollection matches = Regex.Matches(inputString, pattern);
For word boundary matching, use the \b metacharacter −
string pattern = @"\bword\b"; // matches "word" as a complete word only
Using Word Boundaries for Exact Matches
The \b metacharacter represents a word boundary, ensuring that the pattern matches only complete words and not partial matches within larger words −
Example
using System;
using System.Text.RegularExpressions;
namespace RegExApplication {
public class Program {
private static void showMatch(string text, string expr) {
Console.WriteLine("The Expression: " + expr);
MatchCollection mc = Regex.Matches(text, expr);
foreach(Match m in mc) {
Console.WriteLine("Found: " + m);
}
}
public static void Main(string[] args) {
string str = "My make";
Console.WriteLine("Matching words with word boundaries:");
showMatch(str, @"\bmake\b");
}
}
}
The output of the above code is −
Matching words with word boundaries: The Expression: \bmake\b Found: make
Using Different Pattern Matching Approaches
Example
using System;
using System.Text.RegularExpressions;
public class Program {
public static void Main(string[] args) {
string text = "I make cakes and remake them daily";
Console.WriteLine("Original text: " + text);
Console.WriteLine();
// Word boundary matching - exact word only
MatchCollection exactMatches = Regex.Matches(text, @"\bmake\b");
Console.WriteLine("Exact word matches for 'make':");
foreach(Match match in exactMatches) {
Console.WriteLine("Found at position " + match.Index + ": " + match.Value);
}
Console.WriteLine();
// Substring matching - includes partial matches
MatchCollection partialMatches = Regex.Matches(text, "make");
Console.WriteLine("All substring matches for 'make':");
foreach(Match match in partialMatches) {
Console.WriteLine("Found at position " + match.Index + ": " + match.Value);
}
}
}
The output of the above code is −
Original text: I make cakes and remake them daily Exact word matches for 'make': Found at position 2: make All substring matches for 'make': Found at position 2: make Found at position 19: make
Using Case-Insensitive Matching
Example
using System;
using System.Text.RegularExpressions;
public class Program {
public static void Main(string[] args) {
string text = "Make some MAKE and remake";
Console.WriteLine("Original text: " + text);
Console.WriteLine();
// Case-sensitive matching
MatchCollection caseSensitive = Regex.Matches(text, @"\bmake\b");
Console.WriteLine("Case-sensitive matches:");
Console.WriteLine("Count: " + caseSensitive.Count);
// Case-insensitive matching
MatchCollection caseInsensitive = Regex.Matches(text, @"\bmake\b", RegexOptions.IgnoreCase);
Console.WriteLine("Case-insensitive matches:");
Console.WriteLine("Count: " + caseInsensitive.Count);
foreach(Match match in caseInsensitive) {
Console.WriteLine("Found: " + match.Value);
}
}
}
The output of the above code is −
Original text: Make some MAKE and remake Case-sensitive matches: Count: 0 Case-insensitive matches: Count: 2 Found: Make Found: MAKE
Conclusion
Regular expressions in C# provide flexible pattern matching capabilities using the Regex.Matches() method. Use word boundaries \b for exact word matching and consider RegexOptions.IgnoreCase for case-insensitive searches to find substring patterns effectively.
