This works:

String rex = "^\\d+\\.\\s\\p{Lu}+.*";

System.out.println("1. PTYU fmmflksfkslfsm".matches(rex));
// true

System.out.println(". PTYU fmmflksfkslfsm".matches(rex));
// false, missing leading digit

System.out.println("1.PTYU fmmflksfkslfsm".matches(rex));
// false, missing space after .

System.out.println("1. xPTYU fmmflksfkslfsm".matches(rex));
// false, lower case letter before the upper case letters

Breaking it down:

  • ^ = Start of string
  • \d+ = One or more digits (the \ is escaped because it's in a string, hence \\)
  • \. = A literal . (or your original [.] is fine) (again, escaped in the string)
  • \s = One whitespace char (no need for the {1} after it) (I'll stop mentioning the escapes now)
  • \p{Lu}+ = One or more upper case letters (using the proper Unicode escape — thank you, tchrist, for pointing this out in your comment below. In English terms, the equivalent would be [A-Z]+)
  • .* = Anything else

See the documentation here for details.

You only need the .* at the end if you're using a method like String#match (above) that will try to match the entire string.

Answer from T.J. Crowder on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... A compiled representation of a regular expression. A regular expression, specified as a string, must first be compiled into an instance of this class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
May 22, 2026 - In Java, regular expressions are supported through the java.util.regex package, which mainly consists of the following classes: Pattern: Defines the regular expression. Matcher: Used to perform operations such as matching, searching and replacing. PatternSyntaxException: Indicates a syntax error in the regular expression. The Pattern class compiles regex strings into pattern objects.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - A regular character in the Java Regex syntax matches that character in the text. 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 "\".
🌐
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; }
Find elsewhere
🌐
University of Texas
cs.utexas.edu › ~mitra › csFall2007 › cs303 › lectures › regex.html
Regular Expressions in Java
A regular expression is a way of denoting a pattern. In Java this pattern is denoted by a String within the delimiters double quotes (").
🌐
Medium
medium.com › javarevisited › making-regex-your-friend-in-java-ddc5bf7f9a66
Tutorial on how regex works in Java | Javarevisited
September 21, 2021 - Let’s say you get some strings like the following. ... For this you would need to be able to reference the match inside the pattern, this is done using back-references like so. You can see that I have referenced the named capturing group “fruit” at the end of the pattern using the syntax “\\k<NAME>”. That’s the final item for this article. I hope these tools can give you some more confidence when using regular expressions in Java.
🌐
CodeGym
codegym.cc › java blog › strings in java › regular expressions in java
Java RegEx Regular expressions | CodeGym
February 19, 2025 - The pattern ^[0-9]+$ ensures the string is strictly digits, from start to finish. You can also use \\d as shorthand for digits, so the pattern could be "^\\d+$" instead. That’s usually more readable.
🌐
Regexplanet
regexplanet.com › advanced › java › index.html
Java regex testing - RegexPlanet
Online testing for Java (java.util.regex.Pattern) regular expressions.
🌐
Medium
medium.com › @kaustubh.saha › regular-expressions-in-java-b806152aaa93
Regular Expressions in Java. A regular expression is essentially a… | by Kaustubh Saha | Medium
November 29, 2025 - Compiling a pattern is expensive, so if you are using the same regex repeatedly, compile it once and reuse it. Once you have a Pattern, you use it to create a Matcher object based on a specific input string. The Matcher is the engine that actually performs the search against the text, keeping track of where matches are found. import java.util.regex.Pattern; import java.util.regex.Matcher; public class RegexIntro { public static void main(String[] args) { String text = "Java is fun, but Java regex is powerful."; String regex = "Java"; // 1.
🌐
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....
🌐
Reddit
reddit.com › r/learnprogramming › what's the correct syntax for regex in java?
r/learnprogramming on Reddit: What's the correct syntax for regex in java?
February 6, 2026 -

Little context im learning regex in class and my teacher keeps saying that we should always use ^ and $ so the matches() method works but it works just fine without it.
Now idk if using both of them is just good practice, meant for something else or that java used to give wrong outputs from said method without using it?

Edit: turns out its not necessary for the matches() method but it is necessary for Matcher class if you want to find exactly the regex youre using inside a text; "\\d{2}" will return false with the method while the find() method inside Matcher will return true if the text has more than 2 numbers

🌐
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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › regular-expression-in-java-regex-example
Regular Expression in Java: Regex Examples & Tutorial | DigitalOcean
August 3, 2022 - Since java regular expression revolves around String, String class has been extended in Java 1.4 to provide a matches method that does regex pattern matching. Internally it uses Pattern and Matcher java regex classes to do the processing but obviously it reduces the code lines.
🌐
Okta Developer
developer.okta.com › blog › 2022 › 04 › 19 › java-regex
A Quick Guide to Regular Expressions in Java | Okta Developer
April 19, 2022 - For example, if you use the regular expression ab*, you’re issuing an instruction to match a string that has an a followed by zero or more b’s. So strings like ab, abc, abbc, etc. will match our regular expression. The asterisk symbol, *, denotes the number of times a character or a sequence of characters may occur. Regular expressions make finding patterns in text much easier. Some high-level use cases include: ... Regular expressions are also well-supported in many programming languages. Supporting classes for regular expressions in Java are available in the java.util.regex package.
🌐
Regex101
regex101.com
regex101: build, test, and debug 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.
🌐
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.
🌐
Coders Campus
coderscampus.com › home › mastering regular expressions
Mastering Regular Expressions - How to Program with Java
April 9, 2021 - Here's your answer: regular expressions will search through the entire String and doesn't stop when it finds the first occurrence, it will keep searching and tell you about every single match that occurs (including the start and ending indexes). Also, mastering regular expressions means you will have to learn about all of the advanced searching features that exist with regex in Java.
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › howto › Regexe.html
Regular Expression (Regex) Tutorial
Take note that many programming languages (C, Java, JavaScript, Python) use backslash \ as the prefix for escape sequences (e.g., \n for newline), and you need to write "\\d+" instead. See "Python's re module for Regular Expression" for full coverage. Python supports Regex via module re. Python also uses backslash (\) for escape sequences (i.e., you need to write \\ for \, \\d for \d), but it supports raw string in the form of r'...', which ignore the interpretation of escape sequences - great for writing regex.