Welcome to Java's misnamed .matches() method... It tries and matches ALL the input. Unfortunately, other languages have followed suit :(

If you want to see if the regex matches an input text, use a Pattern, a Matcher and the .find() method of the matcher:

Pattern p = Pattern.compile("[a-z]");
Matcher m = p.matcher(inputstring);
if (m.find())
    // match

If what you want is indeed to see if an input only has lowercase letters, you can use .matches(), but you need to match one or more characters: append a + to your character class, as in [a-z]+. Or use ^[a-z]+$ and .find().

Answer from fge on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_matches.asp
Java String matches() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... String regex = "cat|dog|fish"; System.out.println("cat".matches(regex)); System.out.println("dog".matches(regex)); System.out.println("catfish".matches(regex)); System.out.println("doggy bag".matches(regex));
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › matches
Java String matches() - Check Regex Match | Vultr Docs
December 17, 2024 - The matches() method in Java is an essential tool for checking if a given string complies with the pattern defined by a regular expression (regex).
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - Returns the string representation of this pattern. This is the regular expression from which this pattern was compiled. ... Creates a matcher that will match the given input against this pattern. ... Returns this pattern's match flags. ... Compiles the given regular expression and attempts ...
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
This tutorial describes the usage ... covers basic regex syntax, Java’s Pattern and Matcher classes, practical examples for common use cases, and important security considerations. A regular expression (regex) defines a search pattern for strings....
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-matches-method-in-java-with-examples
String matches() Method in Java with Examples - GeeksforGeeks
July 14, 2026 - The matches() method of the String class is used to determine whether a string completely matches a given regular expression (regex). It returns true if the entire string matches the specified pattern; otherwise, it returns false.
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions.
🌐
Programiz
programiz.com › java-programming › library › string › matches
Java String matches()
Java String startsWith() The matches() method checks whether the string matches the given regular expression or not. class Main { public static void main(String[] args) { // a regex pattern for // four letter string that starts with 'J' and end with 'a' String regex = "^J..a$"; System.out....
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › java_string_matches.htm
Java - String matches() Method
regex − the regular expression to which this string is to be matched. This method returns true if, and only if, this string matches the given regular expression. import java.io.*; public class Test { public static void main(String args[]) ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › regular-expression-in-java-regex-example
Regular Expression in Java: Regex Examples & Tutorial | DigitalOcean
August 3, 2022 - Matcher: Matcher is the java regex engine object that matches the input String pattern with the pattern object created. Matcher class doesn’t have any public constructor and we get a Matcher object using pattern object matcher method that takes the input String as argument.
🌐
Jenkov
jenkov.com › tutorials › java-regex › index.html
Java Regex - Java Regular Expressions
March 5, 2019 - The regular expression syntax used by the Java regex API is covered in detail in the text about the Java regular expression syntax · The first thing to look at is how to write a regular expression that matches characters against a given text. For instance, the regular expression defined here: ... will match all strings that are exactly the same as the regular expression.
🌐
Medium
medium.com › javarevisited › making-regex-your-friend-in-java-ddc5bf7f9a66
Tutorial on how regex works in Java | Javarevisited
September 21, 2021 - Essentially there are two ways you can use your regex. One is to match against the whole string.
🌐
W3Resource
w3resource.com › java-tutorial › string › string_matches.php
Java String: matches Method - w3resource
public class MatchesExample { public static void main(String args[]){ String str = new String("The quick brown fox jumps over the lazy dog."); System.out.println(); System.out.print("Regex: (.*)quick brown fox(.*) " ); System.out.println(str.matches("(.*)quick brown fox(.*)")); System.out.print("Regex: (.*)quick brown wolf(.*) " ); System.out.println(str.matches("(.*)quick brown wolf(.*)")); System.out.println(); } }
🌐
Regular-Expressions.info
regular-expressions.info › java.html
Using Regular Expressions in Java
It is important to remember that String.matches() only returns true if the entire string can be matched. In other words: "regex" is applied as if you had written "^regex$" with start and end of string anchors. This is different from most other regex libraries, where the “quick match test” method returns true if the regex can be matched anywhere in the string.
🌐
Baeldung
baeldung.com › home › java › core java › a guide to java regular expressions api
A Guide To Java Regular Expressions API | Baeldung
January 8, 2024 - However, this class has another variant of the compile method that accepts a set of flags alongside the regex argument, which affects the way we match the pattern. These flags are simply abstracted integer values. Let’s overload the runTest method in the test class, so that it can take a flag as the third argument: public static int runTest(String regex, String text, int flags) { pattern = Pattern.compile(regex, flags); matcher = pattern.matcher(text); int matches = 0; while (matcher.find()){ matches++; } return matches; }
🌐
Java String
javastring.net › home › java string matches(regex) examples
Java String matches(regex) Examples
July 29, 2019 - Java String matches(regex) method is used to test if the string matches the given regular expression or not.
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
May 22, 2026 - The Matcher class performs matching operations for input strings. ... import java.util.regex.Matcher; import java.util.regex.Pattern; class MatcherExample { public static void main(String[] args) { Pattern p = Pattern.compile("geeks"); Matcher ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_regular_expressions.htm
Java - Regular Expressions
Index methods provide useful index values that show precisely where the match was found in the input string − · Study methods review the input string and return a Boolean indicating whether or not the pattern is found − · Replacement methods are useful methods for replacing text in an input string − · Following is the example that counts the number of times the word "cat" appears in the input string − · import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { private static final String REGEX = "\\bcat\\b"; private static final String INPUT = "cat
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 7 )
Returns the string representation of this pattern. This is the regular expression from which this pattern was compiled. ... Creates a matcher that will match the given input against this pattern. ... Returns this pattern's match flags. ... Compiles the given regular expression and attempts ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-matches-method-example
Java – String matches() Method example
We are matching the regular expressions(regex) with the input String using the matches() method. public class MatchesExample{ public static void main(String args[]){ String str = new String("Java String Methods"); System.out.print("Regex: (.*)String(.*) matches string?