<img src = "111.gif" alt="图<>片" />
     <img src='222.gif' alt='图片' />
    <img  alt=图片 src= scs.gif />
    <a style="" href="http://www.baidu.com" target="_blank" style="color:Red">百度</a>
    href="http://www.baidu.com"
    <a style='color:Red' href='http://www.baidu.com' target='_blank' >百度</a>
    href='http://www.baidu.com' 
    <a style = color:Red href= http://www.baidu.com target = _blank >百度</a>
    http://www.baidu.com 
    <script type="text/javascript" src="/PointForum/ui/scripts/jsframework.js?version=2009011504"></script>我只想抽取<>内href和src的值,用环视时总是搞搞头晕晕,那位前辈帮我看看!谢谢!

解决方案 »

  1.   

    <[^>]*(src\s*=\s*(['"\s])?(?<src>[^'"\s]*)\1)?(href\s*=\s*(['"\s])?(?<href>[^'"\s]*)\3)?>
      

  2.   

    JQuery能做到吧。 href和src的值? JQuery有个属性的方法 $("#idname").attr("href").val();
      

  3.   


    提取第一个a标签的src值$("a").attr("src"); 
    提取第一个a标签的href值
    $("a").attr("href"); 
      

  4.   


    void Main()
    {
          string html=@" <img src = ""111.gif"" alt=""图<>片"" />
     <img src='222.gif' alt='图片' />
    <img  alt=图片 src= scs.gif />
    <a style="""" href=""http://www.baidu.com"" target=""_blank"" style=""color:Red"">百度</a>
    href=""http://www.baidu.com""
    <a style='color:Red' href='http://www.baidu.com' target='_blank' >百度</a>
    href='http://www.baidu.com' 
    <a style = color:Red href= http://www.baidu.com target = _blank >百度</a>
    http://www.baidu.com 
    <script type=""text/javascript"" src=""/PointForum/ui/scripts/jsframework.js?version=2009011504""></script>";
    Console.WriteLine("\t\tSRC\t\t\tHREF");
    foreach(Match m in Regex.Matches(html,@"(?i)<[^>]*(?:src\s*=\s*(['""\s])?(?<src>[^'""\s]+)\1|href\s*=\s*(['""\s])?(?<href>[^'""\s]+)\2)[^>]*>"))
    {

    Console.WriteLine(string.Format("\t{0}\t\t\t{1}",m.Groups["src"].Value,m.Groups["href"].Value));

    }

    /*
    SRC HREF
    111.gif
    222.gif
    scs.gif
    http://www.baidu.com
    http://www.baidu.com
    http://www.baidu.com
    /PointForum/ui/scripts/jsframework.js?version=2009011504
    */
    }
      

  5.   

    能否不用组来捕获,能这样吗            MatchCollection mc = reg.Matches(test);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);            }
      

  6.   


    可以,这样:
    强劲识别,即使连接中含有'或"也可正确匹配
    static void Main(string[] args)
    {
        string html = @" <img src = ""111.gif"" alt=""图<>片"" />
         <img src='222.gif' alt='图片' />
        <img  alt=图片 src= scs.gif />
        <a style="""" href=""http://www.baidu.com"" target=""_blank"" style=""color:Red"">百度</a>
        href=""http://www.baidu.com""
        <a style='color:Red' href='http://www.b""aidu.com' target='_blank' >百度</a>
        href='http://www.baidu.com' 
        <a style = color:Red href=http://www.baidu.com target = _blank >百度</a>
        http://www.baidu.com 
        <script type=""text/javascript"" src=""/PointFor'um/ui/scripts/jsframework.js?version=2009011504""></script>";
        MatchCollection mc = Regex.Matches(html, @"(?in)(?<=<[^>]*?\s+(src|href)\s*=\s*(?<q>['""])?(?!['""]))(?<find>(?(q).*?(?=\k<q>)|[^\s>]+))");
        foreach (Match m in mc)
        {
            Console.WriteLine(m.Value);
        }
        Console.ReadKey();
    }运行结果:
    111.gif
    222.gif
    scs.gif
    http://www.baidu.com
    http://www.b"aidu.com
    http://www.baidu.com
    /PointFor'um/ui/scripts/jsframework.js?version=2009011504
      

  7.   

    Regex reg = new Regex(@"(?is)<img[^>]*?scr=(['""\s]?)([^'""\s]+)\1[^>]*?>");
            MatchCollection match = reg.Matches(str);
            foreach (Match m in match)
            {
                Response.Write(m.Groups[2].Value + "<br/>");
            }