首页 > 代码库 > 正则匹配 替换..追加..

正则匹配 替换..追加..

这里都是以 图片中的元素为例:

 

匹配出IMG标签中alt的值:

 1 Regex reg = new Regex(@"(?is)(?<=<img[^>]*)[^""]*(?=""[^>]*>)"); 

 

替换alt 内容:

var replace = "alt=‘ 要替换的内容 ";

source = Regex.Replace(source, "alt=\"\"", replace);

上面那个并不严谨

var replace = " 替换内容 ";

source = Regex.Replace(source, @"(?is)(?<=<img[^>]*)[^""]*(?=""[^>]*>)", replace);

自己先定义规则,然后替换

1 Regex r = new Regex(@"(?is)(?<=<img[^>]*)");
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/

 

正则匹配 替换..追加..