本帖最后由 bigsea_99 于 2010-05-31 11:32:10 编辑

解决方案 »

  1.   

    MatchCollection mc = Regex.Match(html,@"(?is)(?<=<img src=(['""])?)(https?)?[^""]+(?:jpg|png|gif)(?=\1)");
    foreach(Match m in mc)
    {
        //m.Value;
    }如果不限制扩展名。MatchCollection mc = Regex.Match(html,@"(?is)(?<=<img src=(['""])?)(https?)?[^""]+(?=\1)");
    foreach(Match m in mc)
    {
        //m.Value;
    }
      

  2.   

    Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc\s*=\s*(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
                MatchCollection mc = reg.Matches(yourStr);
                foreach (Match m in mc)
                {
                    Console.Write(m.Groups["src"].Value + "\n");
                }
    替换就用Regex.Replace
      

  3.   

    提取
    Regex reg = new Regex(@"(?is)<\w+[^>]*?(?:src|background)=(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
    MatchCollection mc = reg.Matches(yourStr);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Groups["src"].Value + "\n";
    }如果是相对地址替换为绝对地址
    Regex reg = new Regex(@"(?is)(<\w+[^>]*?(?:src|background)=(['""]?))([^'""\s>]+\2[^>]*>)");
    string result = reg.Replace(yourStr, "$1http://www.test.com/$3");
      

  4.   

    楼上几位 背景填充方式<td background="xxx.jpg"> 
    这种方式怎么取得呢?
      

  5.   


    MatchCollection mc = Regex.Match(html,@"(?is)(?<=<\w+ background=(['""])?)(https?)?[^""]+(?=\1)");
    foreach(Match m in mc)
    {
        //m.Value;
    }
      

  6.   

    楼上的 background不怎么好用呀。
      

  7.   

            private static void TestRegex11()
            {
                string html = @"楼上几位 背景填充方式<td background=""xxx.jpg"">  
    这种方式怎么取得呢?";
                MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<\w+ background=(['""])?)(https?)?[^""]+(?=\1)");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }        }
      

  8.   

    wuyazhe大哥,你用你的方法试试获取下面的。试试。不出来呀。<td colspan="2" rowspan="2" valign="top" background="images/default_r7_c14_2.jpg" height=480></td><td colspan="2" width=317 height=42 background=images/default_r6_c14.jpg></td>
       <td><td align="center" valign="middle" background="/images/1b3.jpg">
      

  9.   

            private static void TestRegex11()
            {
                string html = @"<td colspan=""2"" rowspan=""2"" valign=""top"" background=""images/default_r7_c14_2.jpg"" height=480></td><td colspan=""2"" width=317 height=42 background=images/default_r6_c14.jpg></td>
       <td><td align=""center"" valign=""middle"" background=""/images/1b3.jpg"">
    ";
                MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<\w+[\s\S]*?background=(['""])?)(https?)?[^""]+(?=\1)");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }        }
      

  10.   

    background=images/default_r6_c14.jpg不带引号的background不出来。
      

  11.   

    另外这个方法MatchCollection mc = Regex.Match(html,@"(?is)(?<=<img src=(['""])?)(https?)?[^""]+(?:jpg|png|gif)(?=\1)");
    foreach(Match m in mc)
    {
        //m.Value;
    }也是没有引号就出不来。
      

  12.   

     #9楼 得分:0回复于:2010-05-31 15:30:50C# code        private static void TestRegex11()
            {
                string html = @"<td colspan=""2"" rowspan=""2"" valign=""top"" background=""images/default_r7_c14_2.jpg"" height=480></td><td colspan=""2"" width=317 height=42 background=images/default_r6_c14.jpg></td>
       <td><td align=""center"" valign=""middle"" background=""/images/1b3.jpg"">
    ";
                MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<\w+[\s\S]*?background=(['""]?))(https?)?[^"">]+(?=\1)");
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }        } 
     
      

  13.   

    private static void TestRegex11()
      {
      string html = @"<td colspan=""2"" rowspan=""2"" valign=""top"" background=""images/default_r7_c14_2.jpg"" height=480></td><td colspan=""2"" width=317 height=42 background=images/default_r6_c14.jpg></td>
      <td><td align=""center"" valign=""middle"" background=""/images/1b3.jpg"">
    ";
      MatchCollection mc = Regex.Matches(html, @"(?is)(?<=<\w+[\s\S]*?background=(['""]?))(https?)?[^"">]+(?=\1)");
      foreach (Match m in mc)
      {
      Console.WriteLine(m.Value);
      }  }  
      

  14.   

     可是这样的还是提取不出来,辛苦了,问题还是有。  <td width=1000 height=92 background=images/yyt2_r1_c1.jpg>
      

  15.   

    你怎么测试的。汗。
    都正常的啊不过我才发现。没看完整题目。
    2个都匹配。这样        private static void TestRegex11()
            {
                string html = @"<td colspan=""2"" rowspan=""2"" valign=""top"" background=""images/default_r7_c14_2.jpg"" height=480></td><td colspan=""2"" width=317 height=42 background=images/default_r6_c14.jpg></td>
       <td><td align=""center"" valign=""middle"" background=""/images/1b3.jpg"">
    <td align=""center"" valign=""middle"" background=/images/1b3.jpg>
    <img src='xxx.jpg'/> ";
                Regex regExp = new Regex(@"(?is)(?<=<\w+[\s\S]*?(?:background|src)=(['""]?))(https?)?[^'"">]+(?=\1)");
                MatchCollection mc = regExp.Matches(html);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
            }
      

  16.   

    好了,谢谢 wuyazhe大哥,请wuyazhe老大,继续把下面的这个一个得取img图片的让他支持不带引号的图片就可以了。Regex reg = new Regex(@"(?is)(?<=<\w+[\s\S]*?src=(['""])?)(https?)?[^""]+(?:jpg|png|gif)(?=\1)");
      

  17.   

    给你的本来就支持不带引号的
    (['""]?)[^'""]*\1就可以匹配"aa"
    'ff'
    xx
    这类似的。
      

  18.   

        string html="<img src=http://www.jznews.com.cn/pic/gg-pw.jpg>";            Regex reg = new Regex(@"(?is)(?<=<\w+[\s\S]*?src=(['""])?)(https?)?[^""]+(?:jpg|png|gif)(?=\1)");            MatchCollection mc = reg.Matches(html);
                foreach (Match m in mc)
                {
                   Console.WriteLine(m.Value);
                }
    不出来
      

  19.   

    如果是按后缀来取就好了,用src 和 background作来条件来取,如下background-image:url('http://www.jznews.com.cn/pic/sy/pl2.jpg')
    background:url(http://www.jznews.com.cn/pic/huamen01.jpg);类似这样的又无法取到了。
      

  20.   

    我的意思是能否直接通过 .jpg .gif 这样的后缀来取到?
    如果不行就算了。目前的正则也可以满足我的作业要求了。谢谢。
      

  21.   

    加上不就可以了?
    (?:jpg|png|gif)
            private static void TestRegex11()
            {
                string html = @"<td colspan=""2"" rowspan=""2"" valign=""top"" background=""images/default_r7_c14_2.jpg"" height=480></td><td colspan=""2"" width=317 height=42 background=images/default_r6_c14.jpg></td>
       <td><td align=""center"" valign=""middle"" background=""/images/1b3.jpg"">
    <td align=""center"" valign=""middle"" background=/images/1b3.jpg>
    <img src='xxx.jpg'/> ";
                Regex regExp = new Regex(@"(?is)(?<=<\w+[\s\S]*?(?:background|src)=(['""]?))(https?)?[^'"">]+(?:jpg|png|gif)
    (?=\1)");
                MatchCollection mc = regExp.Matches(html);
                foreach (Match m in mc)
                {
                    Console.WriteLine(m.Value);
                }
            }
      

  22.   

    http://regex.kingshang.com/Default.aspx?idx=0