簡體   English   中英

正則表達式:在.java文件中查找Java類的模式

[英]Regex: Pattern to find Java class in .java file

在閱讀.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());

    }
}

我最終希望能夠確定類聲明是否具有超類,並且也可以獲取它,但是現在我遇到了很多麻煩(我不應該)來獲取類的名稱。

[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()方法進行實際匹配。

用於類聲明的RegExp:

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*))\\{$";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM