最近在學正規表示式,
看到一個字串看不懂是什麼意思,
@"(?< word >\sc[\w]*)"
有去查表還是搞不清楚,
麻煩大大們幫忙解惑,感恩~
完整的程式碼如下:
string input = "The men's soft tennis team captured the first gold medal for Taiwan " +
"yesterday in the 2010 Asian Games in Guangzhou, China, while their " +
"female counterparts garnered a silver for Taiwan, which is competing " +
"as Chinese Taipei at the regional sport games."; // 原始字串
string pattern = @"(?<word>\sc[\w]*)"; // 規則字串
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase); // 宣告 Regex 忽略大小寫
MatchCollection matches = regex.Matches(input); // 將比對後集合傳給 MatchCollection
int index = 0;
foreach (Match match in matches) // 一一取出 MatchCollection 內容
{
// 將 Match 內所有值的集合傳給 GroupCollection groups
GroupCollection groups = match.Groups;
// 印出 Group 內 word 值
Console.WriteLine(++index + ": " + groups["word"].Value.Trim());
}