【问题标题】:C# ASP.NET regular expression controlC# ASP.NET 正则表达式控件
【发布时间】:2011-03-09 09:44:49
【问题描述】:

我想使用正则表达式从 HTML 和 aspx 文件中删除以下样式标记代码。

<style type="text/css">
    BODY { background-color:white; }
    TH { align:center; background-color:006563; color:white; font-family:arial; font-size:12pt; }
</style>

【问题讨论】:

标签: c# regex


【解决方案1】:
//Remove scripts
str = Regex.Replace(str, "`<script.*?>`.*?`</script>`", "", RegexOptions.Singleline);  

//Remove CSS styles, if any found
str = Regex.Replace(str, "`<style.*?>`(.| )*?`</style>`", "", RegexOptions.Singleline);

//Remove all HTML tags, leaving on the text inside.
str= Regex.Replace(str, "`<(.| )*?>`", "", RegexOptions.Singleline);

//Remove \r,\t,\n
str= str.Replace("\r", "").Replace("\n", "").Replace("\t", "");

【讨论】:

    【解决方案2】:

    以下正则表达式可用于从 Html 代码中删除样式标签。

    System.Text.RegularExpressions.Regex.Replace(HtmlData,
                         @"(<style[^*]*</style>)","",
                         System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    

    检查上面的代码,让我知道它是否适合你。

    【讨论】:

    • 为什么是[^*]?星号在 html 中不是特殊字符。你的意思可能是&lt;style[^&gt;]*&gt;
    • @shivakant: str = Regex.Replace(str, ".*?", "", RegexOptions.Singleline);这很好用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多