为什么这个表达式不遵循贪婪的方法?为什么这个表达式不遵循贪婪的方法?
string input = @"cool man! your dog can walk on water ";
string pattern = @"cool (?<cool>(.*)) (?<h>((dog)*)) (?(h)(?<dog>(.*))) ";
MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace);
foreach (Match match in matches)
{
Console.WriteLine("cool=" + match.Groups["cool"].Value);
Console.WriteLine("dog=" + match.Groups["dog"].Value);
Console.ReadLine();
}
输出:
cool= man! your dog can walk on water dog=
正如你可以看到:自 (狗)组匹配0 times.But,*是贪婪的,为何它没有试图找到最大匹配(狗)是1吗?
任何线索?
?限制贪婪 – 2009-12-26 11:00:32