//用正则来取文章中的图片 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; }
// 建议这样使用循环: Match m = Re.Match(Str); while (m.Success) { // ... m = m.NextMatch(); }
// 或者如1桉所说,用Matches: MatchCollection ms = Re.Matches(Str); foreach (Match m in ms) { Result += m.Groups[i].Value + " ¦"; }
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;
}
Match m = Re.Match(Str);
while (m.Success)
{
// ...
m = m.NextMatch();
}
MatchCollection ms = Re.Matches(Str);
foreach (Match m in ms)
{
Result += m.Groups[i].Value + " ¦";
}