Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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 正则表达式搜索对象实例化不起作用_Java_Regex - Fatal编程技术网

Java 正则表达式搜索对象实例化不起作用

Java 正则表达式搜索对象实例化不起作用,java,regex,Java,Regex,我正在搜索每一行文件,寻找对象实例化 public static void main(String[] args) { String t = " foo = new Mango(kil(tile,go),y,x); //yank before comment String p ="//s+new//s+[a-zA-Z]+/(.*/);"; Pattern pt = Pattern.compile(p); Matcher m = pt.matcher(t);

我正在搜索每一行文件,寻找对象实例化

public static void main(String[] args) {
    String t = " foo = new Mango(kil(tile,go),y,x); //yank before comment

    String p ="//s+new//s+[a-zA-Z]+/(.*/);";

    Pattern pt = Pattern.compile(p);
    Matcher m = pt.matcher(t);

    if(m.find()){
        m.group(0);
        System.out.print("start at " + m.start());
        System.out.print("end at " + m.end());

    }
}

正则表达式与输入不匹配。什么都不会被打印出来。我也试着用括号括起来,但还是不起作用。我已经去掉了正则表达式中的所有括号。

那些斜杠
/
应该是反斜杠
\
,最后两个应该像前两个一样加倍

String p = "\\s+new\\s+[a-zA-Z]+\\(.*\\);";

请注意,类名可以包含
[a-zA-Z]
以外的字符,因此即使将
/
字符更改为\,这也不太正确。