Java regex basic usage problem

Posted by Ernelli on Stack Overflow See other posts from Stack Overflow or by Ernelli
Published on 2010-05-11T06:27:56Z Indexed on 2010/05/11 6:34 UTC
Read the original article Hit count: 273

Filed under:
|

The following code works:

String str= "test with foo hoo";
Pattern pattern = Pattern.compile("foo");
Matcher matcher = pattern.matcher(str);

if(matcher.find()) { ... }

But this example does not:

if(Pattern.matches("foo", str)) { ... }

And neither this version:

if(str.matches("foo")) { ... }

In the real code, str is a chunk of text with multiple lines if that is treated differently by the matcher, also in the real code, replace will be used to replace a string of text.

Anyway, it is strange that it works in the first version but not the other two versions.

© Stack Overflow or respective owner

Related posts about java

Related posts about regex