这代码写的够乱的,挺简单一事让你搞复杂了: string text = "select * from blue order by a"; string pat = @"(?:select)|(?:from)|(?:order)|(?:by)"; // Compile the regular expression. Regex r = new Regex(pat, RegexOptions.IgnoreCase);
foreach(Match m in r.Matches(text)) { foreach(Capture c in m.Captures) Console.WriteLine(c.Value); }
for (int i = 1; i <= 2; i++)为什么是<=2呢? 那不是只能显示2组吗? 加 上order by 不是有4组.
string pat = @"(?:select)|(?:from)|(?:order)|(?:by)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
foreach(Match m in r.Matches(text))
{
foreach(Capture c in m.Captures)
Console.WriteLine(c.Value);
}