• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get regular expression pattern match to work

 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code:


b comes out to false except when s and x are identical.

I thought I knew something about regular expressions, but clearly I am missing something basic.

Where am I going wrong? Thanks in advance. This is such a helpful website.

TDR
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanya Ruttenberg wrote:Here is the code:


b comes out to false except when s and x are identical.


Correct. As explained in the docs.
http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#matches() says: "Attempts to match the entire region against the pattern."

So you can either do matches(".*where.*+") or you can look into other methods, such as find(), if I'm not mistaken.

Or, if your matching requirements are really just String literals, you can skip regex altogether and just use String.contains().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic