【发布时间】:2013-03-31 16:21:10
【问题描述】:
标题我的意思是公共类声明始终相同,但名称不同:
public class <class name>
我想在 Java 源代码(在 JTextArea 中)搜索公共类。
所以首先它会找到:
public class Example1 或 public class Example2 或 public class Example3
等等。
然后我想存储字符串Example1或Example2或Example3
这个程序将用 Java 编写。
谁能帮帮我?
更新:
试了一下,对 Pattern 和 Matcher 类非常陌生。
private String findPublicClass() {
Pattern pattern = Pattern.compile("\\s*public\\s+class\\s+(\\w+)");
Matcher matcher = pattern.matcher(txtSource.getText());
String s = null;
boolean found = false;
while (matcher.find()) {
s = matcher.group(1);
found = true;
}
if(!found)
System.out.println("No public class found.");
return s;
}
【问题讨论】:
-
到目前为止你写过代码吗?如果没有,我建议你尽力而为,然后带着一些示例代码回到这里,这样我们可以更好地帮助你,所以我们知道你至少尝试过。
-
@Franklin 我试了一下。