string text = "This IS a Test OF ALL Caps";
string pattern = @"(\b[^\Wa-z0-9_]+\b)";
MatchCollection matches = Regex.Matches(text, pattern);
// 結果:IS, OF, ALL, CAPS
模式解析:\b 字邊界,[^\Wa-z0-9_] 排除非單字字元、小寫、數字、底線 → 相當於只匹配大寫字母。
string text = "This is A Test of lowercase";
string pattern = @"(\b[^\WA-Z0-9_]+\b)";
MatchCollection matches = Regex.Matches(text, pattern);
// 結果:is, of, lowercase
string text = "This is A Test of Initial Caps";
string pattern = @"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)";
MatchCollection matches = Regex.Matches(text, pattern);
// 結果:This, A, Test, Initial, Caps
首字元:大寫字母;後續字元:非大寫(含小寫、數字、底線等)。
string html = @"first tag textnext tag text";
string pattern = @"]*?HREF\s*=\s*[""']?([^'"" >]+?)['""]?>";
MatchCollection matches = Regex.Matches(html, pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
// Group[1] 捕獲:first.htm, next.htm
使用 Singleline 讓 . 匹配換行,IgnoreCase 忽略大小寫;非貪婪 *? 避免跨標籤匹配。