Skip to content
Wei Li
Go back

A Java Regex Tester

Edit page
Table of Contents

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.

Flags
find(): 2 matchesmatches(): false
Released 2026-08-02, updated 2026-10-16.
MatchGroupValuestart–end
102026-08-029–19
1 year20269–13
2 month0814–16
3 day0217–19
202026-10-1629–39
1 year202629–33
2 month1034–36
3 day1637–39
replaceAll() result
Released 02/08/2026, updated 16/10/2026.
Java
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

About the Author

Wei Li

Wei Li is a passionate software professional who builds enterprise applications with modern technologies. The guides here come from that work: each one explains a feature and shows it in working code.

Read more on the About page, or find Wei Li on GitHub.


Edit page
Share this post:

Previous Post
Java Records: Immutable Data Classes Made Simple
Next Post
Using Vue Components in Posts