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 ... 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));...
Discussions

regex - Java regular expression match - Stack Overflow
I need to match when a string begins ... at the beginning of the string. I have the following string. ... And it does not match. What would a working regular expression be for this problem? ... Read about Java and regex: regular-expressions.info/java.html.... More on stackoverflow.com
🌐 stackoverflow.com
java - Check if a String matches specific regular expression - Stack Overflow
Note how with java you don't have to code the start (^) and end ($) of input, because String.matches() must match the whole string, so start and end are implied. However, this is just a rudimentary regex, because 99D99H99M will pass. More on stackoverflow.com
🌐 stackoverflow.com
regex - Java String.matches() regular expressions - Stack Overflow
I am trying to use the matches() function to check a user-entered password for certain conditions "Contains six alphanumeric characters, at least one letter and one number" Here is my current con... More on stackoverflow.com
🌐 stackoverflow.com
Java regex String.matches("regex") method not working; what am I doing wrong?
I have a java conditional and I want to print a certain line in an xml field as long as it doesn't contain a certain combination of characters. Examples of lines that contain these combinations that I do not want: AA7451EB-E59C-46C4-BCFF-E992ADF5E302 90C8A48D-0396-4C63-9C25-E6EEEAC755AB 162DAE0A-7318-45F0-AD1D-EF8234F45CAE ... if (line.contains("string") && !line.matches... More on reddit.com
🌐 r/javahelp
2
1
July 17, 2017
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - ... Returns this pattern's match flags. ... Compiles the given regular expression and attempts to match the given input against it. An invocation of this convenience method of the form · Pattern.matches(regex, input); behaves in exactly the same way as the expression
🌐
Medium
medium.com › javarevisited › making-regex-your-friend-in-java-ddc5bf7f9a66
Tutorial on how regex works in Java | Javarevisited
September 21, 2021 - In other words, you’re telling the regex to “match against my regex, but save these sub-results for me”. Let’s look at an example. Here we are trying to match a string of the format: DIGIT+DIGIT=DIGIT.
🌐
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. Returns a boolean value (true or false). Uses Java's Regular ...
🌐
Regular-Expressions.info
regular-expressions.info › java.html
Using Regular Expressions in Java
In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That’s right: 4 backslashes to match a single one. The regex \w matches a word character.
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
For example, the Hello World regex matches the "Hello World" string. . (dot) is another example for a regular expression. A dot matches any single character; it would match, for example, "a" or "1". The following tables lists several regular expressions and describes which pattern they would match.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
The matcher() method is used to search for the pattern in a string. It returns a Matcher object which contains information about the search that was performed. The find() method returns true if the pattern was found in the string and false if ...
🌐
Baeldung
baeldung.com › home › java › java string › get the indexes of regex pattern matches in java
Get the Indexes of Regex Pattern Matches in Java | Baeldung
January 8, 2024 - Let’s say we want to extract all “<…>” segments from the string above, such as “<the first value>” and “<the second value>“. To match these segments, we can use regex’s NOR character classes: “<[^>]*>”. In Java, the Pattern and Matcher classes from the Regex API are important ...
🌐
NTU
www3.ntu.edu.sg › home › ehchua › programming › java › Java_Regexe.html
Regular Expression - Java Programming Tutorial
\b[1-9][0-9]*\b matches any number with a non-zero leading digit, separated by spaces from other words, where \b is the position anchor for word boundary, [1-9] is a character class for any character in the range of 1 to 9, and * is an occurrence indicator for zero or more occurrences.
🌐
Go4Expert
go4expert.com › articles › using-regular-expressions-java-t1074
Using Regular Expressions in Java | Go4Expert
July 27, 2006 - Using Regular Expressions in Java JDK versions 1.4.0 and later have comprehensive support for regular expressions through the standard...
🌐
CodeGym
codegym.cc › java blog › strings in java › regular expressions in java
Java RegEx Regular expressions | CodeGym
February 19, 2025 - Let's see: import java.util.regex.Pattern; public class RegexDigitsOnly { public static void main(String[] args) { String pattern = "^[0-9]+$"; String testString = "12345"; boolean matches = Pattern.matches(pattern, testString); System.out....
🌐
OCPsoft
ocpsoft.org › home › tutorials › regular expressions › "or" in regular expressions `||`
Regular Expression for Or | "|" Regex | Regular Expression I | OCPsoft
October 23, 2012 - In Java, for example: ... import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; public class RegexTest { @Test public void testRegex() { assertTrue(stringMatches("I like dogs, but not lions.")); assertTrue(stringMatches("I like penguins, but not lions.")); assertFalse(stringMatches("I like lions, but not penguins.")); } private boolean stringMatches(String string) { return (string.matches("like dogs") || string.matches("like penguins")) && (string.matches("not lions") || string.matches("not tigers")); } }
🌐
Alvin Alexander
alvinalexander.com › blog › post › java › how-find-string-simple-regex-pattern-matcher
A Java String regex pattern search example | alvinalexander.com
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Find out if a String contains a very simple pattern. */ public class PatternMatcherFind { public static void main(String[] args) { String stringToSearch = "Four score and seven years ago our fathers ..."; Pattern p = Pattern.compile("score"); // the pattern to search for Matcher m = p.matcher(stringToSearch); // now try to find at least one match if (m.find()) System.out.println("Found a match"); else System.out.println("Did not find a match"); } }
🌐
Medium
medium.com › stackera › java-regex-part-5-matches-lookingat-find-8033fa9cdebc
Medium
October 18, 2020 - Many such examples can be found in searching, or find and replace features in a text editor such as Microsoft Word, Notepad, and a lot of others. In order to have more flexible and powerful tools for searching data, checking a substring in a string, confirming the existence of a certain substring before performing matching, and so on, we need to apply the Regular Expression Engine in Java which is located in the package java.util.regex...
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - If you'll create a Pattern with Pattern.compile("a") it will only match only the String "a". There is also an escape character, which is the backslash "\". It is used to distinguish when the pattern contains an instruction in the syntax or a ...
🌐
Baeldung
baeldung.com › home › java › java streams › creating stream of regex matches
Creating Stream of Regex Matches | Baeldung
January 8, 2024 - First, we need to define a regex pattern that can match numbers: ... In this regex, \d+ matches one or more digits. The double backslash \\ is used to escape the backslash character because it’s a special character in Java.