日期:2014-05-17  浏览次数:20531 次

还求个正则,擦,今天下午和正则卯上了
string str = "<div class=""detail_r1_title""><H1>测试品</H1></div>";
 Regex re = new Regex(@"<div class=""detail_r1_title""><H1>\S+</H1></div>", RegexOptions.None);
 Match ma = re.Match(str);  
//ma.Groups[0].Value;
为啥ma.Groups.Count的结果是1,并且值为<div class="detail_r1_title"><H1>测试品</H1></div>
而并不是我想要的 “测试品”


------解决方案--------------------
Regex re = new Regex(@"<div class=""detail_r1_title""><H1>([\\S\\s]*?)</H1></div>", RegexOptions.None);

这个样子应该就可以了····
------解决方案--------------------
如果匹配正确(完整匹配的话)的ma.Groups[0]是肯定有的。

改成这样 Regex re = new Regex(@"<div class=""detail_r1_title""><H1>(\S+)</H1></div>", RegexOptions.None);

ma.Groups[1].Value;//测试品
------解决方案--------------------
你这个根本就没分组,即使分组,Groups[0]是全匹配的

Regex re = new Regex(@"<div class=""detail_r1_title""><H1>(?<value>\S+)</H1></div>"
去ma.Groups[1]

C# code

            string source = @"<div class=""detail_r1_title""><H1>测试品</H1></div>";
             Regex reg = new Regex(@"<div class=""detail_r1_title""><H1>(?<value>.*)</H1></div>");
            Match mm = reg.Match(source);
            MessageBox.Show(mm.Groups["value"].Value;

------解决方案--------------------
探讨

你这个根本就没分组,即使分组,Groups[0]是全匹配的

Regex re = new Regex(@"<div class=""detail_r1_title""><H1>(?<value>\S+)</H1></div>"
去ma.Groups[1]

C# code

string source = @"<div class=""detail_r1_t……

------解决方案--------------------
你的正则不对。。。
改成:
Regex re = new Regex(@"(?<=<div class=""detail_r1_title""><H1>).*?(?=</H1></div>)", RegexOptions.None);