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]
以外的字符,因此即使将/
字符更改为\,这也不太正确。