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


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


source


The method Matcher.matches()

tries to match the entire string against the pattern. Change your template to:



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

      

+8


source







All Articles