When using matches(), you are asking if the string you provided matches your regex as a whole. It is as if you added ^ at the beginning of your regex and $ at the end.
Your regex is otherwise fine, and returns what you expect. I recommend testing it regexplanet.com, Java mode. You will see when matches() is true, when it false, and what each find() will return.
To solve your problem, I think you only need to remove the if (matcher.matches()) condition.
Regexplanet
regexplanet.com › advanced › java › index.html
Java regex testing - RegexPlanet
Online testing for Java (java.util.regex.Pattern) regular expressions.
LDDGO
lddgo.net › en › string › regex
Java Regular Expression Test
Java regular expression online testing provides Java regular expression online matching and replacement functions. Enter the string to match in the input content. Enter a valid Java regular expression in the regular expression,eg \d+ to match number.
How do I write a regex pattern in Java code?
Use Pattern.compile() and Matcher from java.util.regex to define and apply regex.
qodex.ai
qodex.ai › home › all tools › java regex tester
Java Regex Tester, Test Patterns Live Online
Does this tool use the Java regex engine?
This tool runs in the browser using the JavaScript regex engine, which is similar to Java but has some differences. Possessive quantifiers (*+, ++), atomic groups (?>...), and some Unicode properties (\p{IsGreek}) work in Java but not in JavaScript. Always verify critical patterns in a Java runtime.
qodex.ai
qodex.ai › home › all tools › java regex tester
Java Regex Tester, Test Patterns Live Online
How do I test if a string matches a pattern exactly?
Use "yourString".matches("yourPattern") to check for a full match.
qodex.ai
qodex.ai › home › all tools › java regex tester
Java Regex Tester, Test Patterns Live Online
Java Regex Tester
java-regex-tester.appspot.com
Java regex tester
Testing your Regular Expression Pattern
DigitalOcean
digitalocean.com › community › tutorials › regular-expression-in-java-regex-example
Regular Expression in Java: Regex Examples & Tutorial | DigitalOcean
August 3, 2022 - Master regular expressions in Java with Pattern and Matcher classes. Complete regex tutorial with examples for validation, searching, and text processing.
RegExr
regexr.com
RegExr: Learn, Build, & Test RegEx
RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).
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.
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
You first create a Pattern object that defines the regular expression using Pattern.compile(). This Pattern object allows you to create a Matcher object for a given string. This Matcher object then allows you to perform various regex operations on a String. package de.vogella.regex.test; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Demonstrates advanced regex operations using Pattern and Matcher classes.
Regex Generator
regex-generator.olafneumann.org
Regex Generator - Creating regex is easy again!
A tool to generate simple regular expressions from sample text. Enable less experienced developers to create regex smoothly.
Myregexp
myregexp.com
Regular Expression Tester
To test JAVA regular expression you can use java-applet
OneCompiler
onecompiler.com › java › 3w9mntts7
Java Online Compiler
OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs.