Learn Java
import com.learnjava.advancedjava.Regex;

Regular Expressions in Java

@Concept

A regular expression is a pattern describing a set of matching text, and Java supports them through the java.util.regex package: Pattern.matches(regex, input) checks whether an entire input matches a given pattern, and String itself offers convenient shortcuts like matches(regex), replaceAll(regex, replacement), and split(regex) that use regular expressions directly, without needing the Pattern class explicitly for simple cases. Common regex building blocks include a backslash followed by the letter d for any digit, a backslash followed by the letter w for any word character (a letter, digit, or underscore), a plus sign meaning one or more of whatever came right before it, and a question mark meaning zero or one.

Within a Java string literal, a backslash must be doubled to survive as one literal backslash in the resulting text, since the string literal's own escaping resolves before the regex engine ever sees the pattern at all. That's why a Java regex matching a run of digits is written with two backslash characters in the source, not one, even though the underlying regex itself only uses a single escape.

@HowItWorks
1

The core regex API

Pattern.matches(regex, input) checks a full match; String's own matches, replaceAll, and split methods use regular expressions directly for common cases.

2

Digit and word-character classes

A backslash followed by the letter d matches any single digit; a backslash followed by the letter w matches any word character — a letter, digit, or underscore.

3

Quantifiers

A plus sign means one or more of whatever came right before it; an asterisk means zero or more; a question mark means zero or one.

4

Doubled backslashes in Java source

A Java string literal needs two backslash characters to represent one literal backslash in the resulting text, since the string literal's escaping resolves before the regex engine ever reads the pattern.

5

replaceAll and split both take a full regex

Not just a literal substring — a period inside either one means any character, not a literal dot, unless it's itself escaped.

@Example
import java.util.regex.Pattern;
 
public class RegexDemo {
public static void main(String[] args) {
String input = "Room 42B";
boolean hasDigits = Pattern.matches(".*\\d+.*", input);
String digitsOnly = input.replaceAll("\\D", "");
System.out.println("Has digits: " + hasDigits);
System.out.println("Digits only: " + digitsOnly);
}
}
Output
Has digits: true
Digits only: 42
1Pattern.matches checks whether the entire input matches the given pattern — here, any characters, then one or more digits, then any more characters.
2"Room 42B" contains the digits 42 in the middle, so the full input does match that pattern, making hasDigits true.
3replaceAll with a pattern matching any non-digit character replaces every letter and space with nothing, leaving only the digits behind.
4Both println calls run once each, showing the match result and the digits extracted from the original text.
@UseCases
Validating that user input matches an expected format, like an email address or phone number
Extracting just the numeric or alphabetic portions of a mixed string
Replacing every occurrence of a pattern, not just a fixed literal substring, throughout a string
Splitting text on a flexible pattern, like any amount of whitespace, instead of one exact character

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.