iT邦幫忙

0

請問一下Regex的字串

最近在學正規表示式,
看到一個字串看不懂是什麼意思,
@"(?< 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());
}
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
八塊鴨肉
iT邦新手 5 級 ‧ 2017-06-04 13:50:08
最佳解答
看更多先前的回應...收起先前的回應...
小魚 iT邦大師 1 級 ‧ 2017-06-04 14:31:27 檢舉

我知道啊,可是我看不懂@"(?< word >\sc[\w]*)"到底是要抓什麼條件,因為抓出來的東西看起來好像沒什麼規律... ...

小魚 iT邦大師 1 級 ‧ 2017-06-04 15:23:09 檢舉

好像有點看懂了, 可是前面那個 < word > 是什麼意思呢?(連在一起會自動消失,在這個網頁看不到),可是字串中如果沒有這一段會報錯...

好了終於過新手階段可以回覆。
(?< word >) 這段指的是這個集合groups的自訂名稱

groups["word"]
小魚 iT邦大師 1 級 ‧ 2017-06-04 19:45:32 檢舉

我看懂了,這個網站講得蠻詳細的,感謝您囉~

我要發表回答

立即登入回答