css类的正则表达式

时间:2012-01-19 09:20:28

标签: java css regex

我正在使用java ....

.MainNav a:hover{ float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; }

上面是css类我想要正则表达式,使得该组包含像

这样的值

group1 = MainNav a:hover

group2 = { float:left; width:70px; height:65px; border-top: 2px Solid #F4E6CC; border-bottom: 2px Solid #805822; border-left: 2px Solid #F4E6CC; border-right: 2px Solid #805822; margin: 0px 0px 0px 0px; align:center; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-weight: bold; color: #FFFFFF; text-decoration: none; text-align: center; background:#C99349; background-image: url(../../images/hor_nav_bg.gif); background-repeat: repeat-X; padding:4px; clear:left; }

意味着类名和其他是定义你能告诉我正则表达式吗?我有点混淆如何为它创建表达式,以便我可以得到输出。

这是我的

代码
package com.tufan.digite.Count;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class GetAllCssFile {
    public static void main(String args[]) throws IOException {
        try {
            FileInputStream fstream = new FileInputStream("D:/digite/work/digite/WEBUI/common/theme1/common.css"); // Get theobject of DataInputStream
            DataInputStream dis = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(dis));
            String strLine;
            while ((strLine = br.readLine()) != null) {
                Matcher matcher = Pattern.compile("([^}]^)({[^}]+})", Pattern.DOTALL | Pattern.MULTILINE).matcher(strLine);
                if (matcher.find()) {
                    String selector = matcher.group(1);
                    String definition = matcher.group(2);
                    System.out.println("selector:" + selector + "definition:"+definition);
                }
            }
        } catch (Exception e) {
            //Do some exception handling here
        }
    }
}

它不会给出任何答案

1 个答案:

答案 0 :(得分:2)

这应该可以完成这项工作,假设您需要使用JavaScript。

var regex = /([^\}\{]+)(\{[^\}]+\})/mi;

这适用于Java:

Matcher matcher = Pattern.compile("([^\\}\\{]+)(\\{[^\\}]+\\})", Pattern.DOTALL | Pattern.MULTILINE).matcher(content);

然后调用matcher.find();将返回组:

if (matcher.find()) {
    String selector = matcher.group(1);
    String definition = matcher.group(2);
}

您的代码的问题在于您要逐行匹配文本。那当然不行。

考虑拥有这样的课程:

public class CssDefinition {
    private String selector;
    private String definition;
    //Appropriate getters and setters go here.
}

以下是代码的正确版本:

public static void main(String args[]) throws IOException {
    FileInputStream fstream = null;
    try {
        fstream = new FileInputStream("D:/digite/work/digite/WEBUI/common/theme1/common.css"); // Get theobject of DataInputStream
        DataInputStream dis = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis));
        StringBuffer buffer = new StringBuffer();
        String strLine;
        while ((strLine = br.readLine()) != null) {
            buffer.append(strLine).append("\n");
        }
        String content = buffer.toString();
        content = content.replaceAll("(?s)/\\*.*?\\*/", ""); //This is to remove the comments from the CSS
        Matcher matcher = Pattern.compile("([^\\}\\{]+)(\\{[^\\}]+\\})", Pattern.DOTALL | Pattern.MULTILINE).matcher(content);
        Set<CssDefinition> definitions = new HashList<CssDefinitions>();
        while (matcher.find()) {
            CssDefinition definition = new CssDefinition();
            definition.setSelector(matcher.group(1));
            definition.setDefintion(matcher.group(2));
            definitions.add(definition);
        }
        //After this the set `definitions` will contain the extracted data
    } catch (Exception e) {
        //Do some exception handling here
    } finally {
        if (fstream != null) {
            fstream.close(); //Always release your resources
        }
    }
}

另请注意,在上面的代码中,我首先删除了CSS中的所有注释。这是为了使代码能够正确解析这样的CSS代码:

b {
    font-weight: bold; /* This is a {test} */
}

由于没有先删除评论,它会给你:

String selector = "b";
String definition = "{\nfont-weight: bold; /* this is a {test}"

而不是:

String selector = "b";
String definition = "{\n\tfont-weight: bold; /* this is a {test}\n}"