这里都是以图片中的元素为例:
匹配出IMG标签中alt的值:
1 Regex reg = new Regex(@"(?is)(?<=<img[^>]*alt="")[^""]*(?=""[^>]*>)");
替换alt 内容:
var replace = "alt='要替换的内容' "; source = Regex.Replace(source, "alt=""", replace);
上面那个并不严谨
var replace = " 替换内容 "; source = Regex.Replace(source, @"(?is)(?<=<img[^>]*alt="")[^""]*(?=""[^>]*>)", replace);
自己先定义规则,然后替换
1 Regex r = new Regex(@"(?is)(?<=<img[^>]*alt="")"); 2 3 var replace = " 替换内容 "; 4 5 source = r.Replace(source, new MatchEvaluator(x => replace));
参考: http://bbs.csdn.net/topics/390320097
https://q.cnblogs.com/q/60920/