c# 正则表达式(转)

1.styleReg:清除样式.如<style>.class{}</style>.全部替换为空.

2.scriptReg和styleReg同样的道理.

3.htmlReg :清除html标签的.输入为<div>aaa</div>,结果为:aaa

4.htmlSpaceReg :html空格&nbsp;替换为空格

5.spaceReg :把一个以上的空格替换为一个空格.

  1. public string RemoveHtml(string src)  
  2. {  
  3.     Regex htmlReg = new Regex(@"<[^>]+>", RegexOptions.Compiled | RegexOptions.IgnoreCase);  
  4.     Regex htmlSpaceReg = new Regex("\\&nbsp\\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);  
  5.     Regex spaceReg = new Regex("\\s{2,}|\\ \\;", RegexOptions.Compiled | RegexOptions.IgnoreCase);  
  6.     Regex styleReg = new Regex(@"<style(.*?)</style>", RegexOptions.Compiled | RegexOptions.IgnoreCase);  
  7.     Regex scriptReg = new Regex(@"<script(.*?)</script>", RegexOptions.Compiled | RegexOptions.IgnoreCase);  
  8.   
  9.     src = styleReg.Replace(src, string.Empty);  
  10.     src = scriptReg.Replace(src, string.Empty);  
  11.     src = htmlReg.Replace(src, string.Empty);  
  12.     src = htmlSpaceReg.Replace(src, " ");  
  13.     src = spaceReg.Replace(src, " ");  
  14.     return src.Trim();    
  15. }  
原文地址:https://www.cnblogs.com/rainbowzc/p/1856688.html