我写有这样的正则表达式 目的是获得FCkeditor中的图片路径 ,可发现 来Src ="*.*"后面得属性也取出来了,align="absMiddle  不知道有没有更准确的方法~~。
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"<img[^>].*?src=""(?<src>[^""].*)""[^>].*?>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
             System.Text.RegularExpressions.MatchCollection m = reg.Matches(str); //设定要查找的字符串
             if (m.Count > 0)
             {
                 return (m[0].Groups["src"].ToString());
             }
             else
             {                  reg = new System.Text.RegularExpressions.Regex(@"<input[^>].*?src=""(?<src>[^""].*)""[^>].*?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                  m = reg.Matches(str);
                 if (m.Count > 0)
                 {
                     return (m[0].Groups["src"].ToString());
                 }
                 else
                 {
                     return "";
                 }             }

解决方案 »

  1.   

    Regex   regex   =   new   Regex(@"<img.*src=[\']?([^\']*)[\']?[^/]*[/]?>”,   RegexOptions.Compiled   |   RegexOptions.IgnoreCase   );   
        
      foreach(Match   matchItem   in   regex.Matche(""))   
      {   
      if(matchItem.Groups[1]!=   null   )   
      {   
      str+=matchItem.Groups[1].Value; 
      }   
      }   
    参考
      

  2.   

    不清楚你的需求,看你的代码猜的MatchCollection mc = Regex.Matches(yourStr, @"(?<=<(?:input|img)(?:(?!src)[\s\S])*src=(['""]?))[^""'\s>]*(?=\1)", RegexOptions.IgnoreCase);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Value + "\n";
    }
      

  3.   

    还是不对 我之前那个 能截取出图片路径 不过连路径后面的也截出来了如:/Upload/images/Article/images_2(5).jpg" vspace="2  我只想要路径 
      

  4.   

    Regex r = new Regex(@"<img src=""(?<src>[^""]*)""+?[^>]*>");
    System.Text.RegularExpressions.MatchCollection m = r.Matches("<img src=\"http://cc.com/aabc.jpg\" vspace=\"2\" />");
    Console.WriteLine(m[0].Groups["src"].ToString());
    ------------------------------------
    输出:
    http://cc.com/aabc.jpg
      

  5.   


    protected void Button1_Click(object sender, EventArgs e)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.163.com");
            request.Timeout = 20000;
            request.ServicePoint.ConnectionLimit = 100;
            request.ReadWriteTimeout = 30000;
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode != HttpStatusCode.OK)
                return;
            StreamReader sr = new StreamReader(response.GetResponseStream());        IList<string> ls = GetPicPath(sr.ReadToEnd());
            for (int i = 0; i < ls.Count;i++ )
            {
                   Response.Write(ls[i].ToString()+"<br/>");        }        Response.End();
        }
        public IList<string> GetPicPath(string M_Content)
        {
            IList<string> im = new List<string>();//定义一个泛型字符类
            Regex reg = new Regex(@"<img.*?src=""(?<href>[^""]*)""[^>]*>", RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(M_Content); //设定要查找的字符串
            foreach (Match m in mc)
            {
                im.Add(m.Groups["href"].Value);
            }
            return im;    }你把"<img.*?src=""(?<href>[^""]*)""[^>]*>
    换成"<input.*?src=""(?<href>[^""]*)""[^>]*>",   就OK