Java Regex Tester for Pattern and Matcher
Test a java.util.regex pattern with named groups, inline flags, \A, \z, \Q…\E quoting and POSIX classes like \p{Alpha}, then copy Pattern.compile code with every backslash already doubled. Paste straight from your Java source if you prefer; the tester can undo the string escaping.
Testing JavaScript, Go or PHP instead? Use the general regex tester.
Click any pattern to load it into the tester
How to use the java regex tester
Matching runs in your browser as you type, so nothing you paste is uploaded.
Enter the regex, not the Java string
Type the pattern as the regex engine sees it:
\d+, not"\\d+". If you copied it out of Java source with doubled backslashes, tick "I pasted this from Java source code" and the tester undoes the escaping for you.Set the flags
iisPattern.CASE_INSENSITIVE,misPattern.MULTILINEandsisPattern.DOTALL. Inline flags like(?i)at the start of the pattern also work.glists every match, like looping overm.find().Paste real input
Use an actual log line, CSV row or request path. Patterns that work on a tidy example and fail on real data almost always trip on whitespace or an optional field.
Check the groups
The Groups tab shows each numbered and named group, which is what
m.group(1)andm.group("name")return.Preview replaceAll
In the Replace tab, use Java replacement syntax:
$1for a numbered group and${name}for a named one. A literal dollar sign is\$.Copy the code
The Code Snippet tab gives you
Pattern.compileandMatchercode with the pattern escaped as a Java string literal and the flags combined with|, which is where most hand-copied patterns break.
Where Java regex differs from the browser engine
Matching here runs on the browser's JavaScript engine. Java-only syntax such as \A, \Z, \z, \Q…\E, \h, \R and POSIX \p{…} classes is translated first. These are the differences that remain, and the tester warns when your pattern uses one.
String escaping
A Java string literal processes backslashes before the regex engine sees them, so the regex \d+ is written "\\d+" in source.
Java 15 text blocks do not help: they process escapes the same way. Copy the code from the Code Snippet tab rather than escaping by hand, and use Pattern.quote() for literal text you did not write.
matches() versus find()
Matcher.matches() and String.matches() require the whole input to match. find() searches for the next match anywhere.
This tester behaves like find(). To preview matches(), wrap the pattern in ^(?:…)$. A pattern that works here can still return false from matches() if it covers only part of the input.
Possessive quantifiers and atomic groups
Java supports *+, ++, ?+ and (?>…), which never give back characters once matched.
The browser engine has neither, so they are previewed as greedy quantifiers and the tester flags it. Results differ only when backtracking would have changed the match.
\Z versus \z
In Java, \z is the absolute end of input and \Z also allows one final line terminator.
Both are translated faithfully here. The usual mistake is in code: use \z when validating whole input such as an ID, or a trailing newline slips through.
Unicode classes
Java's \w and \d are ASCII unless you set Pattern.UNICODE_CHARACTER_CLASS, which matches this preview.
\p{L}, \p{Lu} and other Unicode properties work here. Java-specific names such as \p{javaLowerCase} are not previewed.
Questions people ask
How do I test a Java regex online?
Enter the pattern as the regex engine sees it (single backslashes), set the flags, and paste your input. Matches and groups update live, and the Code Snippet tab produces Pattern.compile code with the backslashes doubled for a Java string.
Does this run real Java?
No. Matching runs on your browser's JavaScript engine after translating Java syntax such as \A, \z, \Q…\E and \p{Alpha}. The tester lists anything it cannot reproduce exactly, such as possessive quantifiers and atomic groups.
How do I escape a regex in Java?
Double every backslash and escape every double quote when the pattern goes into a string literal, so \d+ becomes "\\d+". For text that should match literally, Pattern.quote(text) does the escaping for you.
What is the difference between matches() and find() in Java?
matches() succeeds only if the entire input matches the pattern. find() looks for the next match anywhere in the input and can be called in a loop to get every match.
How do named groups work in Java regex?
Name a group with (?<name>pattern), refer back to it with \k<name>, read it with m.group("name"), and use ${name} in a replaceAll replacement string.