正则表达式非贪婪讨论

用C#写正则表达式时遇到一个问题:

string text = "<a href=\"member.php?s=90e0805740c4bad3aac3930b39c6440e&u=75297\"><b><i>nhughes1</b></i></a>, "
        + "<a href=\"member.php?s=90e0805740c4bad3aac3930b39c6440e&u=132858\">Nurisaiff</a>";

string pattern1= "<a href=\"member.php.+?\">(?<name>.+?[^<>])</a>";
string pattern2="<a href=\"member.php.+?\">(?<name>[^<>].+?)</a>";
Regex reg1 = new Regex(pattern1, RegexOptions.Compiled | RegexOptions.IgnoreCase);
Regex reg2 = new Regex(pattern2, RegexOptions.Compiled | RegexOptions.IgnoreCase);

string result1 = reg1.Match(text ).Groups["name"].Value;
string result2 = reg2.Match(text ).Groups["name"].Value;

//输出结果
//reg1:<b><i>nhughes1</b></i></a>, <a href="member.php?s=90e0805740c4bad3aac3930b39c6440e&u=132858">Nurisaiff
//reg2:Nurisaiff

我想要的结果是reg2得到的,但是具体原因是什么呢?

posted @ 2011-08-11 18:55  younsolider  阅读(1841)  评论(4编辑  收藏  举报