user2085965
user2085965

Reputation: 403

String.matches() is not matching

condition is not matching.

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

Upvotes: 0

Views: 74

Answers (2)

Dici
Dici

Reputation: 25950

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 + ")")

Upvotes: 1

isnot2bad
isnot2bad

Reputation: 24444

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 + "\\)");

Upvotes: 1

Related Questions