我是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“作为output.But,匹配返回false。
发布于 2013-08-31 17:30:40
正则表达式应该是参数http://www.tutorialspoint.com/java/java_string_matches.htm
s.matches ("^Bug [0-9]*")发布于 2013-08-31 17:33:51
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");
}
}https://stackoverflow.com/questions/18546604
复制相似问题