我是Regex的新手。请在下面的代码中纠正我在哪里做错了。 另请在Java-Regex中推荐一些好的书籍/教程。
public class regexx {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Bug 2742";
if("^Bug [0-9]*".matches(s)){
System.out.println("eq");
}else {
System.out.println("nq");
}
}
}
期待" eq"作为输出。但匹配返回false。
答案 0 :(得分:3)
正则表达式应该是参数 http://www.tutorialspoint.com/java/java_string_matches.htm
s.matches ("^Bug [0-9]*")
答案 1 :(得分:2)
public boolean matches(String regex)
String#matches()将正则表达式作为参数,而不是正在进行操作的string
。
您在应用regex
为了澄清,我把那条线分开了。
尝试
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "Bug 2742";
boolean matches = s.matches("^Bug [0-9]*");
if(matches){
System.out.println("eq");
}else {
System.out.println("nq");
}
}