[英]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|private
和class
关键字之间匹配空格时,应使用\\\\s+
和+
量词。 因为您希望那里至少有1
空间。 \\\\s*
将匹配0
空间,这是不正确的,因为publicclass
无效。
除此之外,这里还有更多选择可以考虑。 像static inner classes
或generic 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.