Java Regex: Match Uppercase Letters with Pattern & Matcher (2026)
Last Updated: | Author: Deep Singh
Regex isn't just for validating emails or parsing logs — it's also a quick way to answer simple questions about text. For example: which words in this string are uppercase? Which are lowercase? Which are digits?
In this tutorial, you'll see a small Java program that checks each word in a string and reports whether it's an uppercase letter, using the character class [A-Z]. It's a gentle next step after the general "match any letter" example, and it shows how narrowing the character class changes the result.
How Pattern and Matcher Work
If you've seen Java regex code before, you'll recognise this two-class pattern:
- Pattern — the compiled form of your regular expression. Compile once, reuse many times.
- Matcher — the object that actually performs the match against a specific piece of text.
The workflow:
Pattern p = Pattern.compile("your_regex");
Matcher m = p.matcher("your_text");
boolean matched = m.matches();
The matches() method checks whether the entire input string fits the pattern. For partial matches inside a larger string, use find() instead. Here, we want to test each word fully, so matches() is the right choice.
The Full Code
Here's the complete program. It splits a string into individual words and checks whether each one is a single uppercase letter.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MatchChar {
public static void main(String[] args) {
String pattern = "[A-Z]";
String text = "5 a T u 1 2 U";
Pattern p = Pattern.compile(pattern);
String[] words = text.split("\\s");
for (int i = 0; i < words.length; i++) {
Matcher m = p.matcher(words[i]);
System.out.println(m.matches() + "\t" + words[i]);
}
}
}
Output:
false 5
false a
true T
false u
false 1
false 2
true U
Step-by-Step Walkthrough
Let's go through the code line by line.
1. Defining the Pattern
String pattern = "[A-Z]";
This is a character class that matches exactly one uppercase letter from A to Z. Lowercase letters, digits, and symbols are all excluded.
2. Compiling the Pattern
Pattern p = Pattern.compile(pattern);
Pattern.compile() parses the regex string once and prepares an efficient matcher. Even for tiny programs, this is the correct habit — move compilation outside any loop.
3. Splitting the Text
String[] words = text.split("\\s");
This splits the string on whitespace. In Java, you write "\\s" because the backslash needs escaping inside a string literal. The result is an array: ["5", "a", "T", "u", "1", "2", "U"].
4. Matching Each Word
Matcher m = p.matcher(words[i]);
System.out.println(m.matches() + "\t" + words[i]);
For each word, we get a Matcher and call matches(). Since the pattern matches only single uppercase letters, the result is true for "T" and "U", and false for everything else.
The "\t" between the boolean and the word adds a tab so the output lines up neatly.
5. Why Lowercase Letters and Digits Return false
[A-Z] only covers the uppercase range. The lowercase "a" and "u" don't fall into that range, and neither do the digits "5", "1", and "2". All of them return false.
Try These Variations
Now that you have the working example, it's easy to see how changing one line changes everything. Try these patterns in place of [A-Z]:
[a-z]— matches only lowercase letters. In the current input,aanduwould returntrue.[0-9]— matches only digits. Only5,1, and2would returntrue.[a-zA-Z]— matches any letter regardless of case.[A-Z]+— matches one or more uppercase letters. Now"USA"would match, but"U"would too. Note that"USA1"would still not match becausematches()requires the whole string.[A-Z]{2,}— matches two or more uppercase letters in a row.\\d— a shorthand for any digit, equivalent to[0-9].\\w— any word character: letters, digits, and underscore.
Experimenting with these is the fastest way to build intuition for regex. Change the input string too, and watch how the booleans shift.
Key Takeaways
matches()checks the whole string. Usefind()to search inside a larger text.- Character classes match one character. Add
+or{n,m}to match repeats. - Compile patterns once. Move
Pattern.compile()outside loops. - Escape backslashes. Regex
\sbecomes"\\s"in Java. - Narrow the character class to change which inputs match.
[A-Z]vs[a-z]vs[0-9]are all one character apart but produce very different results. - Print with context. Always show the word alongside the boolean — debugging is much easier this way.
Wrapping Up
You now know how to use Pattern and Matcher to check whether a string matches a specific regex — in this case, a single uppercase letter. This compile-match-print workflow is the same one you'll use for validation, extraction, and parsing throughout your Java career.
Try modifying the pattern, the input string, or both. Add a word with two uppercase letters, or a mixed-case token, and see how the results change. That experimentation is what turns regex from "magic" into a tool you actually understand.
You might also find these helpful:
No comments:
Post a Comment