如何使用 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