2013-02-14 58 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()方法来进行实际匹配。