text中img标签:
<IMG height=375 alt="" src="" width=500>
<IMG alt="" src="">匹配img标签的正则表达式:string Pattern = @"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>";
匹配img 的src的正则表达式:string Pattern = @"(?<=src/s*=/s*[/'/""]?)(?<url>[http/:]?[^'""]+)";
匹配结果:
获取text中img的src地址:
string Pattern = @"(?<=src/s*=/s*[/'/""]?)(?<url>[http/:]?[^'""]+)";
Regex reg = new Regex(Pattern, RegexOptions.IgnoreCase);
MatchCollection colMatch = reg.Matches(text文本);StringBuilder strImage = new StringBuilder();
if (colMatch.Count > 0) { foreach (Match m in colMatch) { strImage.Append(m.Value).Append(',');}
}
匹配text的img,并给img标签加上链接
string Pattern = @"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>";
Regex reg = new Regex(Pattern, RegexOptions.IgnoreCase);
MatchEvaluator myEvaluator=new MatchEvaluator(RepleaceReg); string content = reg.Replace(text文本), myEvaluator);
RepleaceReg函数:
private string RepleaceReg(Match m)
{ string result=m.ToString();if (albumid != -1)
{result="<a href=ImageShow.aspx?albumid=" + albumid + ">" + m + "</a>";
} return result; }