2013-11-01 54 views
1

我想從下面的示例CSS文件中獲取類名。模式匹配從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; 
} 
+0

您可以使用'Search' /'Replace'在大多數IDE的正則表達式,爲什麼寫這麼多的代碼?!在IDE中測試你的CSS文件模式,你當然也可以在你的CSS文件/文件夾中進行文件搜索.. –

+0

僅供參考,你不需要使用DataInputStream;只需將FileInputStream對象('fstream')直接傳遞給InputStreamReader構造函數即可。 DataInputStream和DataOutputStream是專用流,用於讀取和寫入某些種類的二進制數據。賠率是你永遠不會有合法需要使用它們。 –

回答

2

您可以使用這樣的事情:

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)); 
     }  
    } 
} 

該模式查找換行符,可能有一些空格後面跟着一個點,然後是任何非空格或點後跟任何空格並打開的字符{