<a href="www.example.com/News/123.html">这个a标签要</a>
<a href="/News/123123.html">这个a标签要</a>
<a href="/News/">这个a标签不要</a>
<a href="/News/123">这个a标签要</a>
<a href="http://www.test.com/News/123.html">这个a标签要</a>
<a href="http://www.test.com/News/123.aspx">这个a标签不要</a>
<a href="http://www.test.com/News/123.asp">这个a标签不要</a>大概意思就是 一个可以匹配出 href属性里包含某个标识的A标签的href属性和text属性,注意是包含,
而且这个正则需要 还需要一个结束的标识.比如 如上面的例子 包含的标识为/News/
结束的标识为 .html这个结束的标识是可给可不给的,如果有那么就href属性就必须以这个标识为结尾,没有的话就需包含给定的标识就行了.类似于这个正则
 string regString = @"(?is)<a[^>]*href=(['""\s]?)(?<href>"/News/@"([^\.]*\".html@"|((?!"").)+?))\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>";但是这个正则是 必须以/News/为开头的,现在是包含它就行.

解决方案 »

  1.   

    上面那个正则有点问题
    重发一个
    string regString = @"(?is)<a[^>]*href=(['""\s]?)(?<href>/News/([^\.]*\.html|((?!"").)+?))\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>";
      

  2.   

    <a href="/News/">这个a标签不要</a>这个为什么不要?
      

  3.   


    void Main()
    {
       string html=@"
    <a href=""www.example.com/News/123.html"">这个a标签要</a>
    <a href=""/News/123123.html"">这个a标签要</a>
    <a href=""/News/"">这个a标签不要</a>
    <a href=""/News/123"">这个a标签要</a>
    <a href=""http://www.test.com/News/123.html"">这个a标签要</a>
    <a href=""http://www.test.com/News/123.aspx"">这个a标签不要</a>
    <a href=""http://www.test.com/News/123.asp"">这个a标签不要</a>
    "; foreach(Match m in  Regex.Matches(html,@"(?is)<a[^>]*href=(['""]?)[^'""]*/News/[^""'\.]+(\.html)?\1[^>]*>[^<]*</a>"))
    {
      Console.WriteLine(m.Value);
    }
    }/*
    <a href="www.example.com/News/123.html">这个a标签要</a>
    <a href="/News/123123.html">这个a标签要</a>
    <a href="/News/123">这个a标签要</a>
    <a href="http://www.test.com/News/123.html">这个a标签要</a>
    */
      

  4.   


                string str = "<a href=\"www.example.com/News/123.html\">这个a标签要</a>"
                    + "<a href=\"/News/123123.html\">这个a标签要</a>"
                    + "<a href=\"/News/\">这个a标签不要</a>"
                    + "<a href=\"/News/123\">这个a标签要</a>"
                    + "<a href=\"http://www.test.com/News/123.html\">这个a标签要</a>"
                    + "<a href=\"http://www.test.com/News/123.aspx\">这个a标签不要</a>"
                    + "<a href=\"http://www.test.com/News/123.asp\">这个a标签不要</a>";
                Regex reg = new Regex(@"<a[^>]*?href=(['""\s]?)([^'""\s]*?/News/[^'""\.]+(\.html)?)\1[^>]*?>");
                MatchCollection match = reg.Matches(str);
                foreach (Match m in match)
                {
                    Response.Write(m.Groups[2].Value + "<br/>");
                }
    /*
    www.example.com/News/123.html
    /News/123123.html
    /News/123
    http://www.test.com/News/123.html
    */
      

  5.   

    还是huangwenquan123的可以Tim的不知道你的怎么不行, 看了一下huangwenquan123的正则比你多了一些\s 这个是匹配空白字符的 应该就是这个在影响吧.我测试的是这个页面
    http://blog.youdao.com/search?q=%E5%82%A8%E6%B0%94%E7%BD%90&start=0&t=a&keyfrom=blog.page1匹配关键字是 要包含 /blog/static/ 的a标签
      

  6.   

    http://download.csdn.net/source/2844387