16

Suppose, I have a string:

String str = "some strange string with searched symbol";

And I want to search in it some symbols, suppose it will be "string". So we have a following:

str.matches("string"); //false
str.matches(".*string.*"); //true

So, as stated in the title, why I must specify whole string in Java regular expression?

Java documentation says:

public boolean matches(String regex)

Tells whether or not this string matches the given regular expression.

It doesn't says

Tells whether or not this whole string matches the given regular expression.

For example, in the php it would be:

$str = "some strange string with searched symbol";
var_dump(preg_match('/string/', $str)); // int(1)
var_dump(preg_match('/.*string.*/', $str)); //int(1)

So, both of the regex's will be true.
And I think this is correct, because if I want to test whole string I would do str.matches("^string$");

PS: Yes, I know that is to search a substring, simpler and faster will be to use str.indexOf("string") or str.contains("string"). My question regards only to Java regular expression.


UPDATE: As stated by @ChrisJester-Young (and @GyroGearless) one of the solutions, if you want to search regex that is part of a subject string, is to use find() method like this:

    String str = "some strange string with searched symbol";
    Matcher m = Pattern.compile("string").matcher(str);
    System.out.println(m.find()); //true
spirit
  • 3,265
  • 2
  • 14
  • 29
  • you may use `find` func inside `if`. – Avinash Raj Jul 04 '16 at 11:11
  • 2
    Javadoc for class `Matcher` is pretty clear on that: "The matches method attempts to match the entire input sequence against the pattern." – Gyro Gearless Jul 04 '16 at 11:15
  • 4
    Java has few problems with unintuitional methods naming in regex department. For instance `replace(str, str)` vs `replaceAll(str, str)`. It is not obvious that *only* difference between these two is that `replace` doesn't use regex syntax while `replaceAll` does (both methods will replace all occurrences or selected text). Unfortunately all we can do now is just accept this, made our share of errors and finally get used to it. – Pshemo Jul 04 '16 at 11:28
  • Because regular expression "string" doesn't match your test string. The function behaves exactly like documentation says. I suggest reading a bit more about regexes, this should help with future misunderstandings. – MatthewRock Jul 04 '16 at 12:36

3 Answers3

19

matches always matches the whole input string. If you want to allow substrings to match, use find.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
3

As the documentation you suggest,

public boolean matches(String regex)

Tells whether or not this string matches the given regular expression.

What it means is whether that string matches with the given regex. i.e. matches verifies whether your string is an instance of the given regex.

not whether it contains a substring which is an instance of the given regex. As Chris suggested you can use find instead or for your problem you can use contains.

Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
0

Yes, as you already know (now) that matches() will return true only for the complete string.

But, in your update, you have stated that:

As stated by @ChrisJester-Young (and @GyroGearless) the only solution, if you want to search regex that is part of a subject string, is to use find() method...

I would like to say that using find() is not the only solution. There is at least one more, which I know :

String str = "some strange string with searched symbol";
boolean found = str.split("string").length>1;
System.out.println(found);

This prints true. And this will work for all regular expressions. Though this is not the way to do it, and is instead a hack.

There may be many more solutions.

dryairship
  • 6,022
  • 4
  • 28
  • 54
  • I'm pretty sure that there are a lot of **hacks** to achive same result =). I was telling about the *right* way to achive this. But now I understand, that's that was only my opinion. Removed *only* =) – spirit Jul 04 '16 at 13:16
  • @spirit You removed only the only only in your only question? Only? Ok. Only! :P – dryairship Jul 04 '16 at 13:21