简体   繁体   中英

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) !

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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