如何使用正则表达式删除一些css属性?

如何使用正则表达式删除一些css属性?,css,regex,Css,Regex,我把它作为内联css。我想用正则表达式替换以“background”和“font”开头的所有属性的空格。在内联css中,最后一个属性的结尾可能没有分号 我使用这段代码作为django过滤器,使用BeautifulSoup从服务器端删除这些属性 "outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roma

我把它作为内联css。我想用正则表达式替换以“background”和“font”开头的所有属性的空格。在内联css中,最后一个属性的结尾可能没有分号

我使用这段代码作为django过滤器,使用BeautifulSoup从服务器端删除这些属性

"outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left;"

我不知道您的编程环境的细节,但您要求提供一个正则表达式。此正则表达式将属性键(加冒号和任何空格)作为组1(
$1
),属性值作为组2(
$2
):

表达式不会删除属性值。它找到了他们。如何删除它们取决于您的编程环境(语言/库)

但基本上,您将执行全局查找/替换,将整个结果替换为
$1

例如,使用Java可以做到这一点

 ((?:background|font)(?:[^:]+):(?:\\s*))([^;]+)
publicstaticvoidmain(字符串[]args)引发异常{
字符串[]行={
“轮廓样式:无;边距:0px;填充:2px;背景色:#eff0f8;颜色:#3b3a39;字体系列:乔治亚州,'Times New Roman',泰晤士报,衬线;字体大小:14px;字体样式:正常;字体变体:正常;字体重量:正常;字母间距:正常;行高:18px;孤立:2;文本对齐:中心;文本缩进:0px;文本转换:无;白色spac”e:正常;widows:2;字间距:0px;边框:1px实心#ebebeb;浮点:左;“,
“轮廓样式:无;边距:0px;填充:2px;背景色:#eff0f8;颜色:#3b3a39;字体系列:乔治亚州,'Times New Roman',泰晤士报,衬线;字体大小:14px;字体样式:正常;字体变体:正常;字体重量:正常;字母间距:正常;行高:18px;孤立:2;文本对齐:中心;文本缩进:0px;文本转换:无;白色spac”e:正常;widows:2;字间距:0px;边框:1px实心#ebebeb;浮点:左“,
“背景色:#eff0f8;”,
“背景色:#eff0f8”,
};
String regex=“((?:background | font)(?:[^:]+):(?:\\s*)([^;]+)”;
Pattern p=Pattern.compile(regex);
用于(字符串s:行){
StringBuffer sb=新的StringBuffer();
匹配器m=匹配器p;
while(m.find()){
//捕获组(2)仅用于调试目的
//只是为了得到它的长度,这样我们就可以用“-”来填充它
//帮助比较之前和之后
字符串文本=m.group(2);
text=text.replaceAll(“.”,“-”);
m、 附录替换(sb,“$1”+文本);
//对于非调试模式,只需使用此选项即可
//m.B(sb,“$1”);
}
m、 (某人);
System.err.println(“>”+s);//之前
System.err.println(“<”+sb.toString());//之后
System.err.println();
}
}

你是在它上线之前还是在它到达客户端(javascript?)时这样做的。我必须从服务器端解析它。你可能应该重新考虑使用“内联css”来支持可重用类。我得到的内容是TinyMCE粘贴的HTML,由其他网站的用户发布。。我必须替换元素的font*和background*属性,以使内容与我的Web Themegrat表达式兼容。谢谢你的帮助。但是当我用这个正则表达式拆分并将所有拆分的数据连接在一起时,我得到了这个结果。“background*”和“font*”的值没有删除:(我已经修改了表达式并更新了答案,包括一个示例。
 ((?:background|font)(?:[^:]+):(?:\\s*))([^;]+)
public static void main(String[] args) throws Exception {

    String[] lines = {
        "outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left;",
        "outline-style: none; margin: 0px; padding: 2px; background-color: #eff0f8; color: #3b3a39; font-family: Georgia,'Times New Roman',Times,serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 18px; orphans: 2; text-align: center; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; border: 1px solid #ebebeb; float: left",
        "background-color: #eff0f8;",
        "background-color: #eff0f8",
    };

    String regex = "((?:background|font)(?:[^:]+):(?:\\s*))([^;]+)";

    Pattern p = Pattern.compile(regex);

    for (String s: lines) {
        StringBuffer sb = new StringBuffer();
        Matcher m = p.matcher(s);
        while (m.find()) {

            // capturing group(2) for debug purpose only
            // just to get it's length so we can fill that with '-' 
            // to assist comparison of before and after
            String text = m.group(2);
            text = text.replaceAll(".", "-");
            m.appendReplacement(sb, "$1"+text);

            // for non-debug mode, just use this instead
            // m.appendReplacement(sb, "$1");
        }
        m.appendTail(sb);

        System.err.println("> " + s); // before
        System.err.println("< " +sb.toString()); // after
        System.err.println();
    }
}