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.
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.
14:16
Regular Expressions Made Easy with Java - 2019 Tutorials - YouTube
26:29
EVERYTHING you need to know about REGULAR EXPRESSIONS in JAVA in ...
08:21
The Regex in Java | Complete Java Course for Beginners #18 - YouTube
27:33
Java Regular Expressions Tutorial | Regular Expressions in Java ...
12:03
Java Tutorial For Beginners | Regex In Java | Java Regular Expression ...
Top answer 1 of 3
29
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.
2 of 3
2
It depends which method are you using. I think it will work if you use Matcher.find(). It will not work if you are using Matcher.matches() because match works on whole line. If you are using matches() fix your pattern as following:
^\d+\.\s{1}[A-Z]+.*
(pay attention on trailing .*)
And I'd also use \. instead of [.]. It is more readable.
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.
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.
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.
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) ·
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 \\\\.