在 C# 中匹配帶有萬用字元的字串
常用的萬用字元是星號 (*)。它表示一個字串中的零個或多個字元。
在以下示例中,星號用於匹配以 m 開頭並以 e 結尾的單詞——
@”\bt\S*s\b”
以下是完整程式碼——
示例
using System;
using System.Text.RegularExpressions;
namespace Demo {
public class Program {
private static void showMatch(string text, string expr) {
MatchCollection mc = Regex.Matches(text, expr);
foreach (Match m in mc) {
Console.WriteLine(m);
}
}
public static void Main(string[] args) {
string str = "toss cross tacos texas";
Console.WriteLine("Matching words that start with 't' and ends with 's':");
showMatch(str, @"\bt\S*s\b");
}
}
}輸出
Matching words that start with 't' and ends with 's': toss tacos texas
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP