如何使用 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP