Java Regex not working - why?
match.matches () returns false. This is weird because if I take this regex and run a String to rubular.com, two matches are shown. 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");
}
+2
Nick heiner
source
to share
2 answers
Matcher.matches
returns whether the entire region matches a pattern.
Try instead find
. (Of course, this works great with your example.)
+12
Jon Skeet
source
to share
The method Matcher.matches()
tries to match the entire string against the pattern. Change your template to:
".*FTW(((?!ODP).)+)ODP.*"
+8
paxdiablo
source
to share