Ludvig W
Ludvig W

Reputation: 824

Java regex does not match as expected

I'm starting with regex in Java recently, and I cant wrap my head around this problem.

        Pattern p = Pattern.compile("[^A-Z]+");
        Matcher matcher = p.matcher("GETs");
        if (matcher.matches()) {
            System.out.println("Matched.");
        } else {
            System.out.println("Did not match.");
        }

Result: Did not Match(Unexpected result) Explain this

I get the output "Did not match." This is strange to me, while reading https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html, I'm using the X+, which matches "One, or more times".

I thought my code in words would go something like this:

"Check if there is one or more characters in the string "GETs" which does not belong in A to Z."

So I'm expecting the following result:

"Yes, there is one character that does not belong to A-Z in "GETs", the regex was a match."

However this is not the case, I'm confused to why this is. I tried the following:

        Pattern p = Pattern.compile("[A-Z]+");
        Matcher matcher = p.matcher("GETs");
        if (matcher.matches()) {
            System.out.println("Matched.");
        } else {
            System.out.println("Did not match.");
        }

Result: Did not match. (Expected result)

        Pattern p = Pattern.compile("[A-Z]+");
        Matcher matcher = p.matcher("GET");
        if (matcher.matches()) {
            System.out.println("Matched.");
        } else {
            System.out.println("Did not match.");
        }

Result: Matched. (Expected result)

Please, explain why my first example did not work.

Upvotes: 4

Views: 12266

Answers (3)

Pineda
Pineda

Reputation: 7593

Matcher.matches returns true only if the ENTIRE region matches the pattern.

For the output you are looking for, use Matcher.find instead


Explanation of each case:

Pattern p = Pattern.compile("[^A-Z]+");
Matcher matcher = p.matcher("GETs");
  if (matcher.matches()) {

Fails because the ENTIRE region 'GETs' isn't lowercase


Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GETs");
  if (matcher.matches()) {

This fails because the ENTIRE region 'GETs' isn't uppercase


Pattern p = Pattern.compile("[A-Z]+");
Matcher matcher = p.matcher("GET");
  if (matcher.matches()) {

The ENTIRE region 'GET' is uppercase, the pattern matches.

Upvotes: 12

Frigg0
Frigg0

Reputation: 125

if you want a regex to match either in UPPERCASE and lowercase, you can use this:
String test = "yes"; String test2= "YEs"; test.matches("(?i).*\\byes\\b.*"); test2.matches("(?i).*\\byes\\b.*");

will return true in the two cases

Upvotes: 0

Jay Kravetz
Jay Kravetz

Reputation: 233

You're very first regex asks to match any character that is not in an uppercase range of A-Z. The match is on the lowercase "s" in GETs.

Upvotes: 1

Related Questions