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.
Advertisement
Answer
Matcher.matches
returnstrue
only if the ENTIRE region matches the pattern.For the output you are looking for, use
Matches.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.