簡要介紹C#中正則表達式Regex的match和matches方法
string s = "aaaa(bbb)aaaaaaaaa(bb)aaaaaa";
string pattern = "\\(\\w+\\)";
Match result = Regex.Match(s,pattern);
MatchCollection results = Regex.Matches(s,pattern);
然后你會看到
result.Value = {(bbb)};
results[0].Value = {(bbb)};
results[1].Value = {(bb)};
也就是match會捕獲第一個匹配。而matches會捕獲所有的匹配。
——————————————————
matchcollection result = Regex.matches(s)
match類型就是一個單獨的捕獲,matchcollection就是一組捕獲。