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 › Matcher.html
Matcher (Java Platform SE 8 )
July 21, 2026 - java.util.regex.Matcher · All Implemented Interfaces: MatchResult · public final class Matcher extends Object implements MatchResult · An engine that performs match operations on a character sequence by interpreting a Pattern. A matcher is created from a pattern by invoking the pattern's ...
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher("Visit W3Schools!"); boolean matchFound = matcher.find(); if(matchFound) { System.out.println("Match found"); } else { System.out.println("Match not found"); } } } // Outputs Match found ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › matcher-class-in-java
Matcher Class in Java - GeeksforGeeks
January 27, 2026 - The Matcher class provides methods to: ... import java.util.regex.*; public class GFG { public static void main(String[] args) { Pattern pattern = Pattern.compile("java"); Matcher matcher = pattern.matcher("java is java"); System.out.println(matcher.find()); } }
🌐
CodeGym
codegym.cc › java blog › strings in java › java regex - matcher
Java Regex - Matcher
May 30, 2022 - The Matcher class has many methods. The most important of these is the matches() method which returns true if the regular expression matches the text, else returns false. Java Matcher Class has many other useful methods for replacing text in ...
🌐
Regexplanet
regexplanet.com › advanced › java › index.html
Java regex testing - RegexPlanet
Online testing for Java (java.util.regex.Pattern) regular expressions.
🌐
Medium
medium.com › stackera › java-regex-part-5-matches-lookingat-find-8033fa9cdebc
Medium
October 18, 2020 - In order to have more flexible and powerful tools for searching data, checking a substring in a string, confirming the existence of a certain substring before performing matching, and so on, we need to apply the Regular Expression Engine in Java which is located in the package java.util.regex.* In this lecture, we are going to discuss 2 main classes in the expression engine: Pattern and Matcher; and the three foundation methods in the Matcher class: matches(), lookingAt(), and find()
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - Creates a matcher that will match the given input against this pattern. ... Returns this pattern's match flags. ... Compiles the given regular expression and attempts to match the given input against it. An invocation of this convenience method of the form · Pattern.matches(regex, input); ...
🌐
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 - To create a pattern, we must first ... a regular expression as the first argument. Matcher object interprets the pattern and performs match operations against an input String....
🌐
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 › java string › get the indexes of regex pattern matches in java
Get the Indexes of Regex Pattern Matches in Java | Baeldung
January 8, 2024 - In regex, capturing groups play a crucial role by allowing us to reference them later or conveniently extract sub-patterns. To illustrate, suppose we aim to extract the content enclosed between ‘<‘ and ‘>‘. In such cases, we can create a pattern that incorporates a capturing group: “<([^>]*)>”. As a result, when utilizing Matcher.group(1), we obtain the text “the first value“, “the second value“, and so on.
🌐
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 - Unlike some languages where regex is built directly into the string syntax, Java uses a two-class system. Think of it as a two-step dance: A Pattern object is a compiled representation of your regular expression. You don't create it with a constructor; instead, you use the static Pattern.compile() method. 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.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - Below is the list of the most frequently used methods in the Pattern class API for Regex Java. A matcher is the engine that performs Java pattern matching operations on a character sequence by interpreting a Pattern.
🌐
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.
🌐
Jenkov
jenkov.com › tutorials › java-regex › matcher.html
Java Regex - Matcher
November 6, 2017 - The Java Matcher class (java.util.regex.Matcher) is used to search through a text for multiple occurrences of a regular expression.
🌐
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.
Top answer
1 of 4
2

(answer changed after OP added more details)

Your string

String inputText = "\1f\1e\1d\02002868BF03030000000000000000S023\1f\1e\1d\03\0d";

Doesn't actually contains any \ literals because according to Java Language Specification in section 3.10.6. Escape Sequences for Character and String Literals \xxx will be interpreted as character indexed in Unicode Table with octal (base/radix 8) value represented by xxx part.

Example \123 = 1*82 + 2*81 + 3*80 = 1*64 + 2*8 + 3*1 = 64+16+3 = 83 which represents character S

If string you presented in your question is written exactly the same in your text file then you should write it as

String inputText = "\\1f\\1e\\1d\\02002868BF03030000000000000000S023\\1f\\1e\\1d\\03\\0d";

(with escaped \ which now will represent literal).


(older version of my answer)

It is hard to tell what exactly you did wrong without seeing your code. You should be able to find at least \1, \1, \1, \0 since your regex can match one \ and one hexadecimal character placed after it.

Anyway this is how you can find results you mentioned in question:

String text = "\\1f\\1e\\1d\\020028";
Pattern p = Pattern.compile("\\\\[a-fA-F0-9]{2}");
//                                          ^^^--we want to find two hexadecimal 
//                                               characters after \
Matcher m = p.matcher(text);
while (m.find())
    System.out.println(m.group());

Output:

\1f
\1e
\1d
\02
2 of 4
1

You need to read the file properly and replace '\' characters with '\\'. Assume that there is file called test_file in your project with this content:

\1f\1e\1d\02002868BF03030000000000000000S023\1f\1e\1d\03\0d

Here is the code to read the file and extract values:

public static void main(String[] args) throws IOException, URISyntaxException {        
    Test t = new Test();
    t.test();
}

public void test() throws IOException {        
    BufferedReader br =
        new BufferedReader(
            new InputStreamReader(
                getClass().getResourceAsStream("/test_file.txt"), "UTF-8"));
    String inputText;

    while ((inputText = br.readLine()) != null) {
        inputText = inputText.replace("\\", "\\\\");

        Pattern pattern = Pattern.compile("\\\\[a-fA-F0-9]{2}");
        Matcher match = pattern.matcher(inputText);

        while (match.find()) {
            System.out.println(match.group());
        }
    }
}
🌐
GeeksforGeeks
geeksforgeeks.org › java › string-matches-method-in-java-with-examples
String matches() Method in Java with Examples - GeeksforGeeks
July 14, 2026 - It returns true if the string matches the regex, otherwise false. We will use matches() method to check if a string contains only alphabetic characters. ... import java.util.*; public class StringMatches { public static void main(String[] args) ...
🌐
TutorialsPoint
tutorialspoint.com › javaregex › javaregex_matcher_matches.htm
java.util.regex.Matcher.matches() Method
The java.time.Matcher.matches() method attempts to match the entire region against the pattern. Following is the declaration for java.time.Matcher.matches() method. true if, and only if, the entire region sequence matches this matcher's pattern.
🌐
Medium
medium.com › @AlexanderObregon › common-java-regular-expressions-regex-interview-questions-and-answers-59861678272f
Common Java Regular Expressions (Regex) Interview Questions and Answers
March 25, 2024 - Regular expressions, or Regex, are a powerful tool for processing text. In Java, the java.util.regex package provides classes for matching character sequences against patterns specified by regular expressions. This article explores common interview questions and topics related to Java Regular Expressions, offering detailed answers and code examples.