<a href="http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD">艾弗森 </a> 
<a href=http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD>艾弗森 </a>
<a clas="id" href="http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD">艾弗森</a>
@"<a\s+((?!href)[^<>])*href=""(?<href>[^""]+)""[^<>]*>(?<title>[^<>]*)"
@"<a\s+((?!href)[^<>])*href=['""]?(?<href>[^""'<>\s]+)['""]?[^<>]*>(?<title>[^<>]*)"
以上3种A标签形式。要怎么修改以上2个正则表达式才能通过
Groups["href"]和Groups["title"]
获取到http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD  和  艾弗森

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] input = new string[]{@"<a href=""http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD"">艾弗森 </a>",
    @"<a href=http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD>艾弗森 </a> ",
    @"<a clas=""id"" href=""http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD"">艾弗森 </a> "};
                Regex re = new Regex(@"<a\s+((?!href)[^<>])*href=['""]?(?<href>[^""'<>\s]+)['""]?[^ >]*>(?<title>[^<>]*)");  
                foreach(string s in input)
                {
                    Console.WriteLine("{0} {1}",re.Match(s).Groups["href"].Value,re.Match(s).Groups["title"].Value);
                }
               
            }
        }
    }
    http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD 艾弗森
    http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD 艾弗森
    http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD 艾弗森
    Press any key to continue . . .
      

  2.   

    @"(?si)<a\b.*?href\s*=\s*""(?<href>[^""]+)""[^>]*>(?<title>.*?)</a>"
      

  3.   


    <a[\s|\S]*?(?:href=[\"\'])(?<href>[^\"\']*)[^>]*>(?<title>[\s|\S]*?(?=</a>))</a>
      

  4.   

    @"(?si)<a\b.*?href\s*=\s*['""]?(?<href>[^\s'"">]+)[^>]*>(?<title>.*?)</a>"
      

  5.   

     Regex re = new Regex(@"(?i)<a\s+((?!href)[^<>])*href=['""]?(?<href>[^""'<>\s]+)['""]?[^>]*>(?<title>[^<>]*)");  
      

  6.   

                string str = @"<a href=""http://tieba.baidu.com/f?kw=%B0%AC%B8%A5%C9%AD"">艾弗森 </a>"; //三种情况都可以,测试过了            Regex reg = new Regex("<a.*?href=\"?(?<url>.*?)\"?>(?<name>[^>]+)</a>");//
                MatchCollection mc = reg.Matches(str);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Groups["url"].ToString()+"  "+m.Groups["name"].ToString());            }
      

  7.   

    请教
    (?si)是什么意思?
    (?i)是什么意思!
      

  8.   

    (?i)IgnoreCase(忽略大小写) 匹配时不区分大小写 
      

  9.   

    <a class=a href='http://chat.163.com/index.html' target='_blank'>网易聊天站</a></td><td width='25%' height='20'>&nbsp;&nbsp;<a class=a href='http://chat.sina.com.cn/' target='_blank'>新浪聊天</a></td><td width='25%' height='20'>&nbsp;&nbsp;<a class=a href='http://chat.263.net/' target='_blank'>263聊天网络</a></td><td width='25%' height='20'>&nbsp;&nbsp;<a class=a href='http://chat.yinsha.com/' target='_blank'>碧聊—可视语音聊天</a></td></tr>还是这个问题?为什么单独的几条就能读出来?
    以上的这些就一条也读不出来呢?
    高手再帮忙看看
      

  10.   

    try...Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<href>[^'""\s>]+)\1[^>]*>(?<title>.*?)</a>");
    MatchCollection mc = reg.Matches(yourStr);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Groups["href"].Value + "\n";
        richTextBox2.Text += m.Groups["title"].Value + "\n";
    }