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.

Answer from Chop on Stack Overflow
🌐
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.
People also ask

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
🌐
Qodex
qodex.ai › home › all tools › java regex tester
Java Regex Tester, Test Patterns Live Online
Test and debug Java regex patterns in real time. See matches highlighted, capture groups extracted, and flags applied instantly. Free, no signup.
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
The first parameter indicates which ... be case-insensitive. The second parameter is optional. The matcher() method is used to search for the pattern in a string....
🌐
Regex-testdrive
regex-testdrive.com › en
Regex Online Tester | Regular Expression Test Drive
This site allows you to check the operation of the regular expression for Java. Regular expression string specified by the TEST button is tested, I can confirm the result
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
May 22, 2026 - Use Pattern.compile() to create a regex pattern. Use matcher() on a Pattern to perform matches.
🌐
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.
🌐
Regex101
regex101.com › r › hj0miD › 1
regex101: Java Regex
Online regex tester and debugger with real-time match highlighting, detailed explanations, substitutions, unit tests, benchmarking, and code generation. Supports PCRE2, JavaScript, Python, Go, Java, .NET, Rust, POSIX ERE, and POSIX BRE.
🌐
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
🌐
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 - We’ll first create a Pattern object by calling its static compile method and passing it a pattern we want to use. Then we’ll create a Matcher object be calling the Pattern object’s matcher method and passing it the text we want to check for matches.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - Our Java regex cheat sheet offers the correct syntax for Regex Java, including classes and methods, boundary matchers, quantifiers, and more.
🌐
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.
🌐
BrowserStack
browserstack.com › home › guide › how to use pattern.compile in java for advanced regex matching
How to Use Pattern.compile in Java for Advanced Regex Matching | BrowserStack
June 17, 2026 - Pattern pattern = Pattern.compile(“regex”, Pattern.<FLAG>); To use a regex on a string, you need a Matcher object.