Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用reagex检测有效的方法调用_Java_Regex - Fatal编程技术网

Java 使用reagex检测有效的方法调用

Java 使用reagex检测有效的方法调用,java,regex,Java,Regex,我需要编写方法来识别类中的方法调用。 A=新的A(); a、 call() 我需要从我的类中找到a.call()。 [a-zA-Z_]只匹配一个字符。追加*或+以匹配多个字符 匹配任何字符。转义以匹配文字点 尝试以下正则表达式: if (word.matches("^[a-zA-Z_](.)][a-zA-Z_]*$") ) { System.out.println(word); } 例如: "[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(

我需要编写方法来识别类中的方法调用。 A=新的A(); a、 call()

我需要从我的类中找到a.call()。

  • [a-zA-Z_]
    只匹配一个字符。追加
    *
    +
    以匹配多个字符
  • 匹配任何字符。转义
    以匹配文字点
尝试以下正则表达式:

    if (word.matches("^[a-zA-Z_](.)][a-zA-Z_]*$") ) {
          System.out.println(word);
    }
例如:

"[a-zA-Z_]\\w*\\.[a-zA-Z_]\\w*\\(.*?\\)"

请澄清您的具体问题或添加其他详细信息,以突出显示您所需的内容。正如目前所写的,很难准确地说出你在问什么。(从flag对话框复制)这应该适用于您相对简单的示例:
“^[a-zA-Z\+\.[a-zA-Z\+\(\\);$”
,但它找不到包含参数的方法调用,或者对象是表达式结果的情况。这将超出正则表达式自身所能处理的范围。@Chathuranga,您必须知道此代码无法处理
person.sayHello(other.getName())
。(有多对括号)
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());
        }
    }
}