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 › tutorial › essential › regex
Lesson: Regular Expressions (The Java™ Tutorials > Essential Java Classes)
This lesson explains how to use the java.util.regex API for pattern matching with regular expressions. Although the syntax accepted by this package is similar to the Perl programming language, knowledge of Perl is not a prerequisite.
🌐
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:
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
May 31, 2026 - 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.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
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 "\". It is used to distinguish when the pattern contains an instruction in the syntax or a character. Let’s look at an example ...
🌐
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.
Find elsewhere
🌐
LMU
cs.lmu.edu › ~ray › notes › regex
regex
Sometimes there is an explicit compile function or method, and sometimes special syntax is used to compile, such as the very common form /.../. Example: find "color" or "colour" in a given string. // Java Pattern p = Pattern.compile("colou?r"); Matcher m = p.matcher("The color green"); m.find(); ...
🌐
Medium
medium.com › javarevisited › making-regex-your-friend-in-java-ddc5bf7f9a66
Tutorial on how regex works in Java | Javarevisited
September 21, 2021 - Note that these relate to Java and I believe they can differ across languages. Brackets is called a character class while parenthesis is referred to as a capturing group [1]. A character class is a way to say which characters you want to match. For instance. Here the output will be the following. ... A capturing group is something completely different. This is a way for you to say that you want your regex to save what it found so that you can retrieve it later.
🌐
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 - The most basic form of pattern matching supported by the java.util.regex API is the match of a String literal. For example, if the regular expression is foo and the input String is foo, the match will succeed because the Strings are identical:
🌐
Jenkov
jenkov.com › tutorials › java-regex › pattern.html
Java Regex - Pattern
November 6, 2017 - Here is a Pattern.matches() example in Java code: import java.util.regex.Pattern; public class PatternMatchesExample { public static void main(String[] args) { String text = "This is the text to be searched " + "for occurrences of the pattern."; String pattern = ".*is.*"; boolean matches = Pattern.matches(pattern, text); System.out.println("matches = " + matches); } }
🌐
Dev.java
dev.java › learn › regex
Regular Expressions - Dev.java
This section teaches the regular expression syntax supported by the java.util.regex API and presents several working examples to illustrate how the various objects interact.
🌐
Career Karma
careerkarma.com › blog › java › java regex: an introduction
Java Regex: A Complete Guide | Career Karma
July 20, 2022 - One time-saving technique called regex — regular expressions — can be used to find or validate string patterns. This article takes a look at what regular expressions are, how they might be used in Java, and resources to help you with writing Java-flavored regex.
🌐
Studyopedia
studyopedia.com › home › java regular expressions
Java Regular Expressions - Studyopedia
April 4, 2026 - Metacharacters example... false true false false · Above, the metacharacter is \d, but we are using \\d. The reason we write \\d instead of just \d in Java strings come down to how Java handles escape sequences in string literals: In regex, \d is a metacharacter that means “any digit” (0–9).
🌐
Gettysburg College
cs.gettysburg.edu › ~tneller › cs112 › examples › regex-summary.pdf pdf
Java Regular Expressions
(Based on: http://docs.oracle.com/javase/tutorial/essential/regex/examples/RegexTestHarness.java) Basic concepts: http://en.wikipedia.org/wiki/Regular_expression#Basic_concepts ·  · Alphabet of symbols ·  · Concatenation (this then that)  · Decisions (this or that)  ·
🌐
GUVI
guvi.in › blog › java › how to use regular expressions in java: a beginner’s guide to regex
How to Use Regular Expressions in Java: A Beginner’s Guide to Regex
August 4, 2025 - Example: ^java matches “java” at the beginning of a line. Some characters (like . or *) have special meanings in regex. Use a backslash \ to escape them if you want to match the literal character.
🌐
Medium
medium.com › @dattu1993 › understanding-regular-expressions-in-java-a-comprehensive-guide-f24e744ad82a
Understanding Regular Expressions in Java: A Comprehensive Guide | by Venkata Tumuluri | Medium
March 2, 2024 - Remember to escape characters that have special meanings in regex, like the dot `.`. ... You can use flags to modify the behavior of the pattern matching, such as case-insensitive matching. Regular expressions are a potent tool in Java for string processing. While their syntax can be daunting at first, understanding the basics of `Pattern` and `Matcher` classes, along with some common patterns, can unlock a wide range of text processing capabilities.
🌐
Medium
medium.com › @arshikasingh41 › java-regex-17a3ccb652e3
Java Regex. Regex is the short form of regular… | by Arshika Singh | Medium
March 4, 2025 - In this example, the regex pattern “\d+” matches one or more digits. The find() method is utilized to find the next subsequence of the input sequence that matches the pattern. ... Quantifiers specify the number of occurrences of a character or group in a pattern. ... It is a valuable skill for Java developers to understand Java Regex.
🌐
Wikitechy
wikitechy.com › tutorials › java › java-regular-expression
java tutorial - Regular Expressions- By Microsoft Award MVP - tutorial java - java programming - learn java - java for beginners - Learn in 30sec | wikitechy
Below is an example of a regular expression in Java, illustrating the way to identify a string of digits in alphanumeric strings. import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { public static void main( String args[] ) { // String to be scanned to find the pattern.
🌐
Intellipaat
intellipaat.com › home › blog › regular expressions in java (java regex)
Regular Expressions in Java (Java Regex) | Pattern Matching with Examples
November 19, 2025 - Explanation: In the above example, the string input is matched with the regex string using anchors that find the match of end at the end of the input. In regular expressions, certain characters have special meanings; they are meta-characters used to control pattern matching. If you want to match them as normal characters, you must escape them using a backslash (\). Note: In Java, since \ is also an escape character in strings, you need to double it as \\\\.