if (word.matches("^[a-zA-Z_](.)][a-zA-Z_]*$") ) {
System.out.println(word);
}
我需要编写方法来识别类中的方法调用。 例如。 A =新A(); a.call();
我需要在我的班级中找到a.call()表单。
答案 0 :(得分:0)
[a-zA-Z_]
只匹配一个字符。附加*
或+
以匹配多个字符。.
匹配任何字符。转义.
以匹配文字点。尝试使用正则表达式:
"[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(.*?\\)"
示例:
import java.util.regex.*;
class T {
public static void main(String[] args) {
String word = "A a =new A(); a.call();";
Pattern pattern = Pattern.compile("[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(.*?\\)");
Matcher matcher = pattern.matcher(word);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}