2013-02-18 78 views
-1

其實,我建了一個Java代碼來分析下面的文本文件:模式匹配解析器

 (FAMIX.Attribute (id: 22) 
(name 'obj_I') 
(parentType (ref: 11)) 
(declaredType (ref: 27)) 
(isPrivate true) 
    ) 

    (FAMIX.Attribute (id: 38) 
(name 'obj_k') 
(parentType (ref: 34)) 
(declaredType (ref: 43)) 
(isPrivate true) 
    ) 

    (FAMIX.Attribute (id: 56) 
(name 'obj_K') 
(parentType (ref: 46)) 
(declaredType (ref: 43)) 
(isPrivate true) 
    ) 

    (FAMIX.Attribute (id: 73) 
(name 'obj_L') 
(parentType (ref: 64)) 
(declaredType (ref: 45)) 
(isPrivate true) 
    ) 

(FAMIX.Attribute (id: 67) 
(name 'obj_G') 
(parentType (ref: 64)) 
(declaredType (ref: 46)) 
(isPrivate true) 
    ) 

(FAMIX.Attribute (id: 93) 
(name 'classD') 
(parentType (ref: 85)) 
(declaredType (ref: 94)) 
(isPrivate true) 
    ) 

    (FAMIX.Attribute (id: 99) 
(name 'classC') 
(parentType (ref: 86)) 
(declaredType(ref: 86)) 
(isPackage true) 
    ) 

(FAMIX.Attribute (id: 114) 
(name 'classB') 
(parentType (ref: 94)) 
(declaredType (ref: 11)) 
(isPrivate true) 
    ) 

    (FAMIX.Attribute (id: 107) 
(name 'obj_c') 
(parentType (ref: 94)) 
(declaredType (ref: 86)) 
(isPrivate true) 
    ) 

的Java代碼:

// Find Attributes 

Pattern p111 = Pattern.compile("FAMIX.Attribute"); 

Matcher m111 = p111.matcher(line); 
while (m111.find()) { 

    FAMIXAttribute obj = new FAMIXAttribute();    
    Pattern p222 = Pattern.compile("id:\\s*([0-9]+)"); 
    Matcher m222 = p222.matcher(line); 

    while (m222.find()) { 
     System.out.print(m222.group(1)); 
    } 

    while ((line = br.readLine()) != null && !(line.contains("FAMIX"))) { 

     Pattern p333 = Pattern.compile("name\\s*'([\\w]+)\\s*'"); 
     Matcher m333 = p333.matcher(line); 

     while (m333.find()) {  

      System.out.print(m333.group(1)); 
     } 

     Pattern p555 = Pattern.compile("parentType\\s*\\(ref:\\s*([0-9]+)\\)"); 
     Matcher m555 = p555.matcher(line); 
     while (m555.find()) { 
      System.out.print(m555.group(1)); 
     } 

     Pattern p666 = Pattern.compile("declaredType\\s*\\(ref:\\s*([0-9]+)\\)"); 
     Matcher m666 = p666.matcher(line); 
     while (m666.find()) { 
      System.out.print(m666.group(1)); 
     } 

    } 

} // exit from finding Attribute 

輸出:基於

 ***************** Attributes ***************** 
     obj_k 38 34 43 
     obj_L 73 64 45 
     classD 93 85 94 
     classB 114 94 11 

輸出,問題是解析器跳過一些輸出(跳轉)

請讓我知道如果問題不清楚,我會盡力進一步澄清。

+0

的問題是當'line'包含'FAMIX',你沒救了'line'下一次運行,因此跳過了一些組。 – nhahtdh 2013-02-18 18:44:54

回答

0

你忘了正則表達式來檢查IsPrivateIsPackage部分

編輯: 有幾個步驟,會告訴你什麼地方出了錯 添加行的打印輸出,看看到底是什麼線失敗和模式如何看待他們

 // Find Attributes 
       System.out.print("***"+line+"***"); 
       Pattern p111 = Pattern.compile("FAMIX.Attribute"); 
       Matcher m111 = p111.matcher(line); 
       while (m111.find()) { 

"***"會給你確切的開始和該行的結束,關於Java的意識。 有時對於匹配器而言,看起來與眼睛相同的字符是不同的。

編輯2: 您的代碼缺少外循環,其中行首次讀取。 你意識到代碼:

    while ((line = br.readLine()) != null && !(line.contains("FAMIX"))) { 

消耗,其中「FAMIX.Attribute」出現在下一行?如果你在(缺少的)外部循環中做了另一次閱讀,你將會錯過其他所有記錄。

+0

我不想讀這個信息,我只需要名稱+ ID +父類型+聲明類型 – 2013-02-18 18:24:41

+0

仍然有同樣的問題。 – 2013-02-18 18:36:06

+0

同樣的問題,它仍然跳過 – 2013-02-18 18:56:42

0

如果您確信該文件包含的確切格式以線條爲規定:

  • 每個idnameparentTypedeclaredType必須完全在一行中聲明。即你沒有這樣的輸入:

    (FAMIX.Attribute (id: 
    38) 
    (name 
    'obj_k') 
    (parentType 
        (ref: 34)) 
    (declaredType (ref: 43)) 
    (isPrivate true) 
    ) 
    

    但是,這是可以的:

    (FAMIX.Attribute (id: 38) 
    (name 'obj_k') (parentType (ref: 34)) (declaredType (ref: 43)) (isPrivate true)) 
    

這是先決條件低於的修改工作。 這個假設來自您當前的代碼。

String line; 

FAMIXAttribute obj = new FAMIXAttribute(); 
boolean isModified = false; 

while ((line = br.readLine()) != null) { 
    if (line.contains("FAMIX.Attribute")) { 
     if (isModified) { 
      // TODO: Save the previous obj 

      obj = new FAMIXAttribute(); 
      isModified = false; 
     } 
    } 

    // TODO: Add the block of code to parse id here 
    // TODO: Add id attribute to obj, set isModified to true 

    // TODO: Add the block of code to parse other stuffs here 
    // TODO: Add those attributes to obj, set isModified to true 
} 

if (isModified) { 
    // TODO: Save the last obj 
} 
+0

仍然不工作讀取許多信息.. – 2013-02-19 14:33:44