string temp = "<div id=\"area1\"><h6><a href=\"aaa\">hello</a></h6><h4><a href=\"bbb\">hi</a></h4></div>" StringBuilder sb = new StringBuilder();
sb.Append("(id=\"area1\"><h6><a href=\"(?<Url>.+?)\">(?<Title>.+?)</a></h6>)");
sb.Append("|(id=\"area1\">.+?<h4><a href=\"(?<Url>.+?)\">(?<Title>.+?)</a></h4>)"); MatchCollection mc = Regex.Matches(temp, sb.ToString(), RegexOptions.IgnoreCase); sb = null; foreach(Match m in mc)
{
System.Console.Out.WriteLine(m.Groups["Url"].Value);
System.Console.Out.WriteLine(m.Groups["Title"].Value);
}
temp 是html中的一小部分,我要取<h6></h6>和<h4></h4>中两项内容。
组名仅允许有"Url"和"Title",不能有"Url2"/"Title2"之类,也就是说<h6>和<h4>中的内容要作为并列的两项match,我才能通过foreach获得值(就像是用我上面写的表达式各单独匹配了一次的效果,但我需要一次匹配)。我现在的正则表达式是错误的,只能取一个(或)。 不知道我说明白没有-_- 谢谢!

解决方案 »

  1.   

    string temp = "<div id=\"area1\"><h6><a href=\"aaa\">hello</a></h6><h4><a href=\"bbb\">hi</a></h4></div>"; MatchCollection mc = Regex.Matches(temp, @".*\<h6\>(?<Url>.*?)\</h6\>.*\<h4\>(?<Title>.*?)\</h4\>", RegexOptions.IgnoreCase); foreach(Match m in mc)
    {
    Console.WriteLine(m.Groups["Url"].Value);
    Console.WriteLine(m.Groups["Title"].Value);
    }
      

  2.   

    回复人:0009(夏天以南) () 信誉:100  2006-12-5 10:15:35   删除   哦不好意思  
    前面没看清楚你的要求  
    ----------------------------
    thanks :)============================如果有人能确定我的这种要求无法实现,请告诉我,谢谢!不确定的就不要误导我了:)
      

  3.   

    StringBuilder sb = new StringBuilder();
    sb.Append("(id=\"area1\"><h6><a href=\"(?<Url>.+?)\">(?<Title>.+?)</a></h6>)");
    sb.Append("|(id=\"area1\">.+?<h4><a href=\"(?<Url>.+?)\">(?<Title>.+?)</a></h4>)");
    是用"|"的吗?http://www.cnblogs.com/sxlfybb/archive/2005/11/30/287564.html
      

  4.   

    水平太菜,一个正则表达式出不来。
    分两步,第一步找到两个并列的<h6></h6><h4></h4>;第二步匹配url和title。
    string yourStr = ......;
    MatchCollction mc = Regex.Matches(yourStr, "<h6>.+?</h6><h4>.+?</h4>", RegexOptions.IgnoreCase);
    Regex regex = new Regex("<a\\s+href=\"(?<url>.+?)\">(?<title>.+?)</a>", RegexOptions.IgnoreCase);
    MatchCollction mc2;
    foreach(Match m in mc)
    {
        mc2 = regex.Matches(m.Value);
        foreach(Match m2 in mc2)
        {
            m2.Groups["url"].Value;
            m2.Groups["title"].Value;
        }
    }
      

  5.   

    这个正则是正确的了@".*?\<h\d\><a href=""(?<Url>.*?)"">(?<Title>.*?)\</a\>\</h\d\>.*?"
      

  6.   

    string temp = "<div id=\"area1\"><h6><a href=\"aaa\">hello</a></h6><h4><a href=\"bbb\">hi</a></h4></div>";MatchCollection mc = Regex.Matches(temp,
    @".*?\<h\d\><a href=""(?<Url>.*?)"">(?<Title>.*?)\</a\>\</h\d\>.*?"
    , RegexOptions.IgnoreCase);foreach (Match m in mc)
    {
    Console.WriteLine(m.Groups["Url"].Value);
    Console.WriteLine(m.Groups["Title"].Value);
    }
    输出:
    aaa
    hello
    bbb
    hi
      

  7.   

    非常感谢各位的帮助。0009(夏天以南) 的办法对于该段html是有效的(巧妙的),呵呵,但这只是我写的一个简单示例,我水平有限,写的不够完善,其实完整的html可能要比这复杂许多,上面的那段html很可能是这样的<div id="area1">
    <h6><a href="aaa"><img src=""></img></a><a href="aaa">hello</a></h6>
    <h4><a href="bbb">hi</a></h4>
    <div class="list">
    <ul>
    <li><a href="ccc">good</a></li>
    <li><a href="ddd">well</a></li>
    <li><a href="eee">fine</a></li>
    </ul>
    </div>
    </div><li>中的内容都要作为与<h6><h4>中的并列内容得到,那0009(夏天以南)的巧妙的表达式就无能为力了:(。mobydick(敌伯威|我排著队拿著爱的号码牌) 的办法可以做到这一点,但这对于我又不是一种理想的方式(增加我整个项目的设计复杂度,对于一段内容需要定义多个正则)
    想问一点,对于Groups可否遍历其获得每一项的"name"(暂且称之为name,或者key也可以),我试过似乎不可以的(m.Groups.GetEnumerator().Current获得的项不具备"name"属性, Groups集合似乎不是个字典),如果可以得到其"name"属性,那对于我将是非常有用的, 这个问题也就相当于解决了!!再次感谢各位的帮助!!
      

  8.   

    <li>中的内容都要作为与<h6><h4>中的并列内容得到,那0009(夏天以南)的巧妙的表达式就无能为力了:(。
    --------------------string temp = "<div id=\"area1\"><h6><a href=\"aaa\">hello</a></h6><h4><a href=\"bbb\">hi</a></h4></div><div class=\"list\">\n<ul>\n<li><a href=\"ccc\">good</a></li>\n<li><a href=\"ddd\">well</a></li>\n<li><a href=\"eee\">fine</a></li>\n</ul>\n</div>";MatchCollection mc = Regex.Matches(temp,
    @".*?\<a href=""(?<Url>.*?)""\>(?<Title>.*?)\</a\>\.*?"
    , RegexOptions.IgnoreCase);foreach (Match m in mc)
    {
    Console.WriteLine("url: " + m.Groups["Url"].Value);
    Console.WriteLine("title: " + m.Groups["Title"].Value);
    }是这样吗?
      

  9.   

    输出:url: aaa
    title: hello
    url: bbb
    title: hi
    url: ccc
    title: good
    url: ddd
    title: well
    url: eee
    title: fine
      

  10.   

    <a href="aaa"><img src=""></img></a>
    如果是这种情况,你想获得一个什么样的结果?
    title是<img src=""></img>还是空
      

  11.   

    0009(夏天以南)===================================================应该是这样的
    <h6><a href="aaa"><img src=""></img></a><a href="aaa">hello</a></h6>
    我要得到的title值应为hello呵呵,我觉得一个正则是解决不了的,比方说再复杂些<div id="area1">
    <h6><a href="aaa"><img src=""></img></a><a href="aaa">hello</a></h6>
    <h4><a href="bbb">hi</a></h4><a href="xxx">其他地址</a><div class="list">
    <ul>
    <li><a href="ccc">good</a></li>
    <li><a href="ddd">well</a></li>
    <li><a href="eee">fine</a></li>
    </ul>
    </div>
    </div>我需要的是<h6><h4><div class="list">这几部分中的链接地址和文本
    <a href="xxx">其他地址</a>是我不需要的
      

  12.   

    我需要的是<h6><h4><div class="list">这几部分中的链接地址和文本
    <a href="xxx">其他地址</a>是我不需要的------------------------你的具体需求我也不是很清楚,可能很复杂,但是我可以给你提供一个思路,你自己再试着扩展
    @".*?\<(h4).*?\><a href=""(?<Url>.*?)"">(?<Title>.*?)\</a\>\</(\1)\>.*?"
    @".*?\<(h4|h6).*?\><a href=""(?<Url>.*?)"">(?<Title>.*?)\</a\>\</(\1)\>.*?"
    @".*?\<(h4|h6|li).*?\><a href=""(?<Url>.*?)"">(?<Title>.*?)\</a\>\</(\1)\>.*?"
    你试一下这三个正则输出结果的区别。用以下数据做测试
    string temp = "<div id=\"area1\"><h6><a href=\"aaa\">hello</a></h6><h4><a href=\"bbb\">hi</a></h4></div><div class=\"list\">\n<ul>\n<li><a href=\"ccc\">good</a></li>\n<li><a href=\"ddd\">well</a></li>\n<li><a href=\"eee\">fine</a></li>\n</ul>\n</div>";