Match方法只返回一个匹配,用Matches方法

解决方案 »

  1.   

        //用正则来取文章中的图片 
        public static string RegexStr(string Str, string Exp) 
        { 
            string Result = ""; 
            System.Text.RegularExpressions.Regex Re = new System.Text.RegularExpressions.Regex(@Exp); 
            System.Text.RegularExpressions.Match m = Re.Match(Str); 
            for (int i=0; i  <= m.Captures.Count; i++) 
            { 
                Result += m.Groups[i].Value + " ¦"; 
                m = m.NextMatch();        } 
            return Result; 
        } 
      

  2.   

    // 建议这样使用循环:
    Match m = Re.Match(Str); 
    while (m.Success) 
    {
      // ...
      m = m.NextMatch();
    }
      

  3.   

    // 或者如1桉所说,用Matches:
    MatchCollection ms = Re.Matches(Str);
    foreach (Match m in ms)
    {
      Result += m.Groups[i].Value + " ¦";  
    }