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]あなたが思っているものと一致していません。これは文字クラスであり、内のこれらの文字のいずれかに一致しますpublicpipe代替単語を照合するために使用する必要があります。

super次の正規表現を使用して、クラスまたはインターフェイスを検討するように拡張できます。-

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

とキーワードの間のスペースを一致させる場合は、数量詞\\s+とともに使用する必要があることに注意してください。少なくともそこにスペースを期待しているからです。は無効であるため、スペースと一致しますが、これは正しくありません。+public|privateclass1\\s*0publicclass

それとは別に、ここで検討するオプションがいくつかあります。static inner classes、またはのようにgeneric classes、そこに小さな変更が必要になります。これは自分で行うことができます。アプローチ方法について少し洞察を与えました。


また、もう1つ重要なことがあります。グループから結果を取得する前に、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 に答える