2

我想在阅读 .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());

    }
}

我最终希望能够确定一个类声明是否有一个超类并得到它,但现在我在获取类名时遇到了足够的麻烦(我不应该这样做)。

4

2 回答 2

11

[public]与您认为的不匹配。它是一个字符类,匹配public. 您应该使用pipe来匹配替代词。

您可以使用以下正则表达式,扩展以考虑super类或接口:-

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

请注意,在匹配和关键字之间的空格时,应使用\\s+with+量词。因为您希望那里至少有空间。将匹配空格,这是不正确的,因为它是无效的。public|privateclass1\\s*0publicclass

除此之外,这里还可以考虑更多选项。像static inner classes, 或generic classes, 这将需要在那里进行小的修改,您可以自己进行。我刚刚对如何处理提供了一些见解。


此外,还有一件更重要的事情。在从组中获取结果之前,您应该调用Matcher#find()方法来进行实际匹配。

于 2013-02-14T22:39:20.480 回答
0

类声明的正则表达式:

private String classRegExp = "(((|public|final|abstract|private|static|protected)(\\s+))?(class)(\\s+)(\\w+)(<.*>)?(\\s+extends\\s+\\w+)?(<.*>)?(\\s+implements\\s+)?(.*)?(<.*>)?(\\s*))\\{$";

对于接口声明:

private String interfaceRegExp = "(((|public|final|abstract|private|static|protected)(\\s+))?interface(\\s+)(\\w+)(<.*>)?(\\s+extends\\s+\\w+)?(<.*>)?(\\s+implements\\s+)?(.*)?(<.*>)?(\\s*))\\{$";

对于枚举声明:

private String enumRegExp = "((((public|final|private|static|protected)(\\s+))?enum(\\s+)(\\w+)?(\\s+implements\\s+\\w+)?(.*)?\\s*))\\{$";
于 2018-12-20T13:13:34.313 回答