อักขระตัวแทนที่ใช้ทั่วไปคือเครื่องหมายดอกจัน (*) มันแสดงถึงอักขระศูนย์หรือมากกว่าในสตริงของอักขระ
ในตัวอย่างต่อไปนี้ เครื่องหมายดอกจันใช้เพื่อจับคู่คำที่ขึ้นต้นด้วย 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