如何在C#中使用正则表达式查找匹配的子字符串?
我们的字符串是-
string str = " My make ";
使用以下正则表达式查找子字符串“make”
@"\bmake\b"
这是完整的代码-
示例
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 start with 'm' and ends with 'e':"); showMatch(str, @"\bmake\b"); } } }
输出结果
Matching words start with 'm' and ends with 'e': The Expression: \bmake\b Found:make