简体   繁体   English

String.matches()不匹配

[英]String.matches() is not matching

condition is not matching. 条件不匹配。

string1 = "k"
string2 = "k(1)"
String regex = "\\\d+";
if ((string1 +"("+regex+")").matches(string2)) {
    return true;
}

Other way round. 另一边。 It's s.matches(regexp) , not regexp.matches(s) ! 它是s.matches(regexp) ,而不是regexp.matches(s)

You should also escape the round brackets ('(' and ')') as they have a special meaning in regular expressions. 您还应该转义圆括号('('和')'),因为它们在正则表达式中具有特殊含义。 So it should be: 所以应该是:

string2.matches(Pattern.quote(string1) + "\\(" + regex + "\\)");

You are interverting the argument. 您正在颠覆论点。 The regex must be passed in parameter of the matches method and the method must be called on the String to be parsed : 正则表达式必须在matches方法的参数中传递,并且必须在要解析的String上调用该方法:

string2.matches(string1 + "(" + regex + ")")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM