Here's the briefest way to code the regex:

Copyif (str.matches("(?!$)(\\d+D)?(\\d\\d?H)?(\\d\\d?M)?"))
    // format is correct

This allows each part to be optional, but the negative look ahead for end-of-input at the start means there must be something there.

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. The regex for a valid format would be:

Copyif (str.matches("(?!$)(\\d+D)?([0-5]?\\dH)?([0-5]?\\dM)?"))
    // format is correct

This restricts the hours and minutes to 0-59, allowing an optional leading zero for values in the range 0-9.

Answer from Bohemian on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_matches.asp
Java String matches() Method
The matches() method searches a string for a match against a regular expression, and returns the matches. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
October 20, 2025 - An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression ... Returns true if and only if this string contains the specified sequence of char values.
Discussions

java - Check if a String matches specific regular expression - Stack Overflow
I am not so good with regular expressions and stuff, so I need help. I have to check if a input value matches a specific regular expression format. Here is the format I want to use, 25D8H15M. Here ... More on stackoverflow.com
🌐 stackoverflow.com
java - What's the difference between String.matches and Matcher.matches? - Stack Overflow
What's the difference between String.matches and Matcher.matches? Is there any difference in terms of performance or other things? More on stackoverflow.com
🌐 stackoverflow.com
arrays - How to use regex with String.matches in java - Stack Overflow
Use "^[A-Z]+\\.[0-9]+\\b" pattern and make sure you run the find() method of the Matcher object to find partial matches inside strings. .matches() finds the entire string matches only. Note that \b word boundary must be defined as "\\b" inside a Java string literal. More on stackoverflow.com
🌐 stackoverflow.com
Match statements in Java
pattern match is good when you're matching against patterns. otherwise, as another comment said, it's just a glorified if statement More on reddit.com
🌐 r/ProgrammingLanguages
14
9
February 15, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-matches-method-in-java-with-examples
String matches() Method in Java with Examples - GeeksforGeeks
November 20, 2024 - In this example, we will check if a string contains only digits. ... // Java program to demonstrate matches() method import java.util.*; public class StringMatches { public static void main(String[] args) { // Declare and initialize a String String s = "12345"; // Check if the string matches the regex for digits boolean r = s.matches("\\d+"); // Matches one or more digits System.out.println("" + r); } }
🌐
Programiz
programiz.com › java-programming › library › string › matches
Java String matches()
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.println("Java".matches(regex)); } } // Output: true
🌐
Codecademy
codecademy.com › docs › java › strings › .matches()
Java | Strings | .matches() | Codecademy
March 18, 2023 - The .matches() method checks whether a string matches a given regular expression, returning true or false. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
Find elsewhere
🌐
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).
🌐
Scaler
scaler.com › home › topics › matches() in java
matches() in Java | matches() Function in Java - Scaler Topics
May 5, 2024 - The matches() method in Java is a method of the String class that checks whether a string matches a given regular expression.
🌐
Tutorialspoint
tutorialspoint.com › home › java › java string matches
Java String Matches
September 1, 2008 - Discover how to utilize the Java String matches() method to validate strings against regular expressions with practical examples.
🌐
Wrox
p2p.wrox.com › java-basics › 63803-regular-expressions-string-matching.html
Regular expressions and string matching
Programmer forums, Software Development, Web Development, developer resources, coding answers, blogs, articles, for programmers in ASP NET, C#, Visual Basic, Java, PHP, MySQL, Ruby, JavaScript, HTML, XML, SharePoint, .NET, CSS, Twitter, WordPress, Joomla, Iphone SDK, iPad applications, android ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_matches.htm
Java - String matches() Method
The result "true" is returned when the string to be matched contains only numbers and, "false" is returned when alphabets and numerals; numerals and characters are combined because it doesn't match the regular expression. package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { String RegexExpression = "^[0-9]+$"; System.out.println("The returned value is: " + "8785".matches(RegexExpression)); System.out.println("The returned value is: " + "@%*6575".matches(RegexExpression)); System.out.println("The returned value is: " + "675hjh".matches(RegexExpression)); } }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › match
String.prototype.match() - JavaScript | MDN
In the match result above, 'see Chapter 3.4.5.1' is the whole match. 'Chapter 3.4.5.1' was captured by (chapter \d+(\.\d)*). '.1' was the last value captured by (\.\d). The index property (22) is the zero-based index of the whole match. The input property is the original string that was parsed.
🌐
Learnsic
learnsic.com › blog › understanding-the-java-string-matches-method
Understanding the Java String Matches Method
May 6, 2024 - This website uses cookies to ensure you get the best experience & by continuing to use our website, you agree to our Privacy and Cookie Policy · Got it
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
December 9, 2025 - 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 ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.string.matches
String.Matches(String) Method (Java.Lang) | Microsoft Learn
Tells whether or not this string matches the given regular expression. An invocation of this method of the form str.matches(regex) yields exactly the same result as the expression · <blockquote> java.util.regex.Pattern.java.util.regex.Patt...
🌐
Reddit
reddit.com › r/programminglanguages › match statements in java
r/ProgrammingLanguages on Reddit: Match statements in Java
February 15, 2024 -

Hey y'all, I'm building a language that transpiles to Java and runs the Java code using it's compiler.

I recently wrote a 'when' statement, taking inspiration from Rust's pattern matching, example:

when a {
        == 10 -> {
                let b: Int = 15        
                let z: Int = 15
                when z {
                        == 5 -> {
                                let g: Int = 69
                        }
                }
        },
        > 10 -> {
                let c: Int = 20
        },
        ? -> {  }
}```

The problem is, that this, and if statements, transpile to the exact same thing, but I wanted to give it a different use case. My first idea was to make a switch statement out of it, but switch statements don't allow for range or operstors, so how would I make it different?
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
The matcher() method is used to search for the pattern in a 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(); } }