views:

114

answers:

2

match.matches() returns false. This is odd, because if I take this regex and test String to rubular.com, is shows two matches. What am I doing wrong?

 Pattern regex = Pattern.compile("FTW(((?!ODP).)+)ODP");
 Matcher match = regex.matcher("ZZZMMMJJJOOFTWZMJZMJODPZZZMMMJJJOOOFTWMZJOMZJOMZJOODPZZZMMMJJJOO");

 if (match.matches()) {
  System.out.println("match found");
 }
 else {
  System.out.println("match not found");
 }
+8  A: 

Matcher.matches returns whether or not the whole region matches the pattern.

Try using find instead. (Certainly with your example, this works fine.)

Jon Skeet
+4  A: 

The Matcher.matches() method tries to match the entire string to the pattern. Change your pattern to:

".*FTW(((?!ODP).)+)ODP.*"
paxdiablo