Type a pattern the way you’d write it in Java, without the extra string-literal backslashes, and the tester shows what find() and matches() return, every group, and the result of replaceAll(). The Java code at the bottom has the escaping done for you.
find(): 2 matchesmatches(): falseReleased 2026-08-02, updated 2026-10-16.
| Match | Group | Value | start–end |
|---|---|---|---|
| 1 | 0 | 2026-08-02 | 9–19 |
| 1 year | 2026 | 9–13 | |
| 2 month | 08 | 14–16 | |
| 3 day | 02 | 17–19 | |
| 2 | 0 | 2026-10-16 | 29–39 |
| 1 year | 2026 | 29–33 | |
| 2 month | 10 | 34–36 | |
| 3 day | 16 | 37–39 |
replaceAll() result Pattern pattern = Pattern.compile("(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group() + " at " + matcher.start());
}
String result = matcher.replaceAll("${day}/${month}/${year}"); Runs on your browser's regex engine after translating Java-only syntax (\A, \Z, \Q…\E, \p{Alpha}, COMMENTS mode and more). Possessive quantifiers and atomic groups can't be emulated, and CASE_INSENSITIVE also folds non-ASCII letters, which Java only does with UNICODE_CASE.
Running a pattern in Java
Java 9 added stream-friendly methods to Matcher. Pick your Java version once and every example on this page switches with it; the choice is remembered on your next visit.
Collecting all matches
List<String> dates = new ArrayList<>();
Matcher matcher = Pattern.compile("\\d{4}-\\d{2}-\\d{2}").matcher(text);
while (matcher.find()) {
dates.add(matcher.group());
}Replacing with a computed value
Matcher matcher = Pattern.compile("\\b[a-z]").matcher(text);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group().toUpperCase());
}
matcher.appendTail(sb);
String result = sb.toString();In both versions, the replacement text is still read as a replacement string, so a $ or \ in it is treated as a group reference or escape. Wrap computed text in Matcher.quoteReplacement(...) when it might contain either.
Common surprises
matches()vsfind().String.matchesandMatcher.matches()require the whole input to match.find()looks for the pattern anywhere. The tester shows both.- Double escaping. The regex
\d+is written"\\d+"in Java source, and a literal backslash is"\\\\". Text blocks don’t help here: they process escapes the same way. $without MULTILINE matches at the very end or just before a final line break, so"abc\n"matchesabc$. Use\zfor the absolute end.- Compile once.
String.matchesandString.replaceAllcompile the pattern on every call, and so doessplitunless the separator is a single plain character. In a loop, keep astatic final Patterninstead.