Pattern & Matcher in Java
From the Java cheat sheet · Regular Expressions · verified Jul 2026
Pattern & Matcher
Java regex via String helpers and the Pattern/Matcher API.
java
// Quick Reference
"abc123".matches("[a-z]+\\d+"); // true (whole string)
"a1b2".replaceAll("\\d", "#"); // "a#b#"
"a,b;c".split("[,;]"); // ["a", "b", "c"]💡 In Java source, regex backslashes are doubled: \d for a digit, \s for whitespace.
⚡ String.matches() must match the ENTIRE string; Matcher.find() matches any substring.
📌 Compile a Pattern once and reuse it in loops - compiling is relatively expensive.
🟢 Groups are 1-indexed; group(0) (or group()) is the full match.
regexpattern