2013-02-14 60 views
1

我想在通過.java文件讀取時查找類的名稱。我就不會回來,現在比賽使用正則表達式:正則表達式:在.java文件中查找Java類的模式

\\s*[public][private]\\s*class\\s*(\\w*)\\s*\\{ 

這是到目前爲止我的代碼:

import java.io.*; 
import java.util.*; 
import java.util.regex.*; 


public class HW4Solution { 

    public static void main(String [] args){ 
     //Prompt user for path to file. 
     File file = null; 
     Scanner pathScan = new Scanner(System.in); 
     while (file == null || !file.exists()) 
     { 
      System.out.print("Enter valid file path: "); 
      file = new File(pathScan.next()); 
     } 
     pathScan.close(); 
     System.out.println("File: " + file.getPath() + " found."); 

     //Read file line by line into buffered reader 
     StringBuffer componentString = new StringBuffer(100); 
     String currentLine; 
     BufferedReader bufferedReader = null; 
     try { 
      bufferedReader = new BufferedReader(new FileReader(file.getPath())); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     //TODO: Find class declarations 
     //TODO: Find superclasses 
     //TODO: Find instance variable declarations 
     //TODO: Find method signatures 
     //TODO: Find access modifier 
     //TODO: Find return type 
     //TODO: Find method name 

     //Creating patterns to look for! 
     //Class declarations 
     Pattern classDeclarationPattern = Pattern.compile("\\s*[public][private]\\s*class\\s*(\\w*)\\s*\\{"); 
     try { 
      while((currentLine = bufferedReader.readLine()) != null){ 
       Matcher classDeclarationMatcher = classDeclarationPattern.matcher(currentLine); 
       if(classDeclarationMatcher.group(1) != null){ 
        componentString.append("Found class declaration: " + classDeclarationMatcher.group(3) + "\n"); 
        /*if(classDeclarationMatcher.group(5) != null){ 
         componentString.append("\tsuperclass: " + classDeclarationMatcher.group(5) + "\n"); 
        }*/ 
        System.out.println(classDeclarationMatcher.group()); 
       } 
      } 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
     finally{ 
      try{ 
       if (bufferedReader !=null) { 
        bufferedReader.close(); 
       } 
      } 
      catch(IOException e){ 
       e.printStackTrace(); 
      } 
     } 

     System.out.println(componentString.toString()); 

    } 
} 

我最終希望能夠確定一個類聲明有一個超類和得到這一點,但現在我有足夠的麻煩(我不應該)得到這個班的名字。

+2

你永遠不會在匹配器上調用'find()'。這意味着你不在尋找結果。 – jlordo 2013-02-14 22:38:18

+3

@iTech吧? '[class]'絕對不是他正在尋找的 – 2013-02-14 22:38:56

+2

@ kmaz13你意識到這實質上是一種啓發式方法,並且會有誤報(也可能是假陰性)? – 2013-02-14 22:40:11

回答

8

[public]與您認爲它應該不匹配。它是一個角色類,它匹配public中的任何一個字符。您應該使用pipe來匹配替代單詞。

您可以使用下面的正則表達式,擴展到考慮super類或接口: -

Pattern.compile("\\s*(public|private)\\s+class\\s+(\\w+)\\s+((extends\\s+\\w+)|(implements\\s+\\w+(,\\w+)*))?\\s*\\{"); 

需要注意的是,public|privateclass關鍵字之間的匹配空間時,你應該使用\\s++量詞。因爲您期望至少有1空間。 \\s*將匹配0空間,這是不正確的,因爲publicclass無效。

除此之外,在這裏可以考慮更多的選擇。像static inner classesgeneric classes,這將需要在那裏進行微小的修改,你可以自己做。我剛剛就如何解決問題提供了一點見解。


另外,還有一件重要的事情。在從組中獲得結果之前,應該調用Matcher#find()方法來進行實際匹配。