java - 模式匹配仅从 CSS 文件中获取类名

标签 java css regex

我想从下面的示例 CSS 文件中获取类名。

.labelIcon{
    background-image: url(../images/eportal/Label-icon.png); 
    background-repeat: no-repeat; 
    height: 16px;
   }
  .appsCSS tr:nth-child(1){
  border: 1px solid #AAAAAA;
  border-top: none; 
  background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05,  #aae3fd));
vertical-align:top;
}
  div.outer {  
  display: block; width:960px;height:1030px;position:relative;
 }
 .dijitButton {
font-family:'Times New Roman',Times,serif;
color:white;                    
 }
 .claro .dijitTitlePane{
    background:-o-linear-gradient(bottom, #2789c6 5%, #79bcff 100%);
    background:-moz-linear-gradient( center top, #2789c6 5%, #79bcff 100% ) !important;
    background-color:#2789c6 !important;
    }
    input[type="text"]:focus,
    select:focus,
    textarea:focus{
      background-color: #f6fcfe;
    }
    #eGrid {
      width: 70em;
      height: 30em;
    }
   .cssmenu ul,
   .cssmenu li, {
    margin: 0;
    padding: 0;
    position: relative;
   }
* {
    border: 0;
    font-family: inherit;
   }
  :focus {
    outline: 0;
   }
    ul {
        list-style-type: none;
     }
       #login .controlbar {
    padding: 10px;
         font-weight: bold;
         }

我只想要上面的这些类名,labelIcondijitButton , 以及前缀为 "." 的名称和后缀 "{"

我尝试了下面的正则表达式代码,但没有得到正确的输出..

String dirFile=workspace + File.separatorChar + projectName + File.separatorChar +Constants.WEBCONTENT + File.separatorChar +"style";
final File folder = new File(dirFile);
if(folder.isDirectory()){
    String result = listFilesForFolder(folder,workspace,projectName);
    if(result != ""){
        Pattern pattern = Pattern.compile("(\\.)(.*?)(\\{)",Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE );
            Matcher matcher = pattern.matcher(result);
            String classNames="";
            while(matcher.find())
            {
                classNames+=matcher.group(2)+"*";
            }
            System.out.println("class: "+classNames);
    }else{
        return;
     }
}
private String listFilesForFolder(File folder, String workspace, String projectName) {
    String allLines = "";
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForFolder(fileEntry,workspace,projectName);
        } else {
            String FileName = fileEntry.getName();
            String filePath=workspace + File.separatorChar + projectName + File.separatorChar +Constants.WEBCONTENT + File.separatorChar +"style"+ File.separatorChar+FileName;
            File f = new File(filePath);
            if(f.length() > 0){
                String strLine;
                FileInputStream fstream = null;
                try {
                    fstream = new FileInputStream(filePath);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                                    // use DataInputStream to read binary NOT text
                // DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

                try {
                    while ((strLine = br.readLine()) != null){
                        allLines += strLine.trim() +"\n";
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                System.out.println("File exist with Content "+allLines);
            }
        }
    }
    return allLines;
}

最佳答案

你可以这样使用:

public class TestRegexp {
    public static void main(String[] args) throws Exception {
        Scanner scanner = new Scanner(new FileReader("test.css"));
        String match = null;
        Pattern pattern = Pattern.compile("\\n\\s*(\\.[^\\. ]+)\\s*\\{");
        while ((match = scanner.findWithinHorizon(pattern, 0)) != null) {
            Matcher matcher = pattern.matcher(match);
            matcher.find();
            System.out.println(matcher.group(1));
        }       
    }
}

该模式寻找一个换行符,该换行符可能有一些空格后跟一个点,然后是任何不是空格的字符或一个点后跟任何空格和开头 {

关于java - 模式匹配仅从 CSS 文件中获取类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19721820/

相关文章:

java - 如何围绕未正确发布的值演示竞争条件?

java - SpringBootApplication 在 ComponentScanning 时排除其他 @SpringBootApplications

html - HTML 页面周围的边框不起作用

css - 悬停颜色不使用 LESS css 改变

regex - 如何使用 sed 只删除双空行?

java - 迭代同步块(synchronized block)中检索的列表是线程安全的吗?

Java HttpSession

html - css 样式内容属性

java - 正则表达式解析包含格式文本的多行条目的问题

regex - MongoDb 未在查询说明符中注册字段