Java Regular Expression Tester
Test Java regular expressions (java.util.regex) against any input, highlight all matches, and try replaceFirst / replaceAll with $1, $2 group references.
\d, not \\d). Supports \p{Alpha}, \Q…\E, \A \Z \z, \h \R, possessive quantifiers, atomic groups, inline flags… Press Enter to evaluate; with the COMMENTS flag Enter inserts a new line (use Ctrl+Enter to evaluate).$1, $2, ${name}. \t, \n, \r are supported. Escape a literal dollar sign as \$ and a backslash as \.Result
How this tester works
Browsers do not ship a Java virtual machine, so this page translates your java.util.regex pattern into an equivalent JavaScript RegExp and runs it locally. Java-only constructs (\p{Alpha}, \Q…\E, \A, \Z, \z, \h, \R, octal escapes, inline flags, Java's definition of \s, . and $) are converted exactly. A few constructs have no JavaScript equivalent and are approximated: when that happens the result panel lists the approximations so you know where the behaviour might differ from Java. The translated JavaScript pattern is shown with every result.
The four buttons mirror the java.util.regex.Matcher API: Test match loops over find() and lists every match with its groups; matches() tells whether the entire input matches; Replace first and Replace all apply replaceFirst() / replaceAll() with Java replacement-string semantics.
Java Pattern — Documentation
| Construct | Matches |
|---|---|
| Characters | |
x | The character x. |
\\ | The backslash character. |
\0n, \0nn, \0mnn | The character with octal value 0n / 0nn / 0mnn (0 ≤ m ≤ 3, 0 ≤ n ≤ 7). |
\xhh, \uhhhh, \x{h…h} | The character with hexadecimal value hh / hhhh / any code point (Java 7+). |
\t \n \r \f \a \e | Tab, line feed, carriage return, form feed, alert (bell), escape. |
\cx | The control character corresponding to x. |
| Character classes | |
[abc], [^abc], [a-zA-Z] | Simple class, negation, range. |
[a-d[m-p]] | Union: a through d, or m through p (same as [a-dm-p]). |
[a-z&&[def]] | Intersection: d, e or f. Approximated in this tester (JavaScript has no intersection). |
[a-z&&[^bc]] | Subtraction: a through z except b and c. Approximated. |
| Predefined character classes | |
. | Any character except line terminators (\n \r \u0085 \u2028 \u2029); with DOTALL, any character. |
\d / \D | A digit [0-9] / a non-digit. |
\h / \H | Horizontal whitespace [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000] / non-horizontal whitespace. |
\s / \S | Whitespace [ \t\n\x0B\f\r] / non-whitespace. (Java's \s is ASCII-only unless UNICODE_CHARACTER_CLASS; this tester reproduces that.) |
\v / \V | Vertical whitespace [\n\x0B\f\r\x85\u2028\u2029] / non-vertical whitespace (Java 8+). |
\w / \W | A word character [a-zA-Z_0-9] / a non-word character. |
\R | Any Unicode line break: \r\n or one of [\n\x0B\f\r\x85\u2028\u2029] (Java 8+). |
\X | An extended grapheme cluster (Java 9+). Approximated as a base character with its combining marks. |
| POSIX character classes (US-ASCII only, unless UNICODE_CHARACTER_CLASS) | |
\p{Lower} / \p{Upper} | A lower-case [a-z] / upper-case [A-Z] letter. |
\p{ASCII} | All ASCII [\x00-\x7F]. |
\p{Alpha} | An alphabetic character [\p{Lower}\p{Upper}]. |
\p{Digit} | A decimal digit [0-9]. |
\p{Alnum} | An alphanumeric character [\p{Alpha}\p{Digit}]. |
\p{Punct} | Punctuation: one of !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~. |
\p{Graph} | A visible character [\p{Alnum}\p{Punct}]. |
\p{Print} | A printable character [\p{Graph}\x20]. |
\p{Blank} | A space or a tab [ \t]. |
\p{Cntrl} | A control character [\x00-\x1F\x7F]. |
\p{XDigit} | A hexadecimal digit [0-9a-fA-F]. |
\p{Space} | A whitespace character [ \t\n\x0B\f\r]. |
| java.lang.Character classes | |
\p{javaLowerCase}, \p{javaUpperCase} | Equivalent to Character.isLowerCase() / isUpperCase() (Unicode aware). |
\p{javaWhitespace} | Equivalent to Character.isWhitespace(). |
\p{javaMirrored}, \p{javaLetter}, \p{javaDigit}, \p{javaLetterOrDigit}… | Other Character.isXxx() methods. |
| Unicode scripts, blocks, categories and binary properties | |
\p{IsLatin}, \p{script=Greek} | A character of the Latin / Greek script. |
\p{InGreek}, \p{block=Cyrillic} | A character in the Greek / Cyrillic block. JavaScript has no blocks: common blocks are translated to code-point ranges, others are approximated by the script of the same name. |
\p{Lu}, \p{L}, \p{N}, \p{P}, \p{gc=Sc} | A character of the given Unicode general category (upper-case letter, letter, number, punctuation, currency symbol…). |
\p{IsAlphabetic}, \p{IsDigit}, \p{IsLetter}, \p{IsUppercase}, \p{IsWhite_Space}… | Unicode binary properties. |
\P{…}, [^\p{…}] | Negation of any of the above. |
| Boundary matchers | |
^ / $ | The beginning / end of a line (with MULTILINE) or of the input. Java's $ also matches before a final line terminator. |
\b / \B | A word boundary / a non-word boundary. |
\A | The beginning of the input (regardless of MULTILINE). |
\G | The end of the previous match. Not supported (ignored). |
\Z | The end of the input but for the final terminator, if any. |
\z | The absolute end of the input. |
| Quantifiers | |
X? X* X+ X{n} X{n,} X{n,m} | Greedy: once or not at all; zero or more; one or more; exactly n; at least n; between n and m times. |
X?? X*? X+? X{n}? X{n,}? X{n,m}? | Reluctant (lazy): the same, matching as little as possible. |
X?+ X*+ X++ X{n}+ X{n,}+ X{n,m}+ | Possessive: the same, never giving back characters (no backtracking). Approximated as greedy in this tester. |
| Logical operators, groups and back-references | |
XY, X|Y | X followed by Y; either X or Y. |
(X) | X as a capturing group (numbered from 1 by opening parenthesis). |
(?<name>X) | X as a named capturing group (Java 7+). Names must start with a letter and contain only letters and digits. |
\n, \k<name> | Whatever the n-th / named capturing group matched. |
| Quotation | |
\ | Nothing, but quotes the following character (\., \[, \$…). It is an error to use a backslash before an alphabetic character that does not denote an escaped construct. |
\Q … \E | Nothing, but quotes all characters between \Q and \E literally (same as Pattern.quote()). |
| Special constructs (named-capturing and non-capturing) | |
(?:X) | X as a non-capturing group. |
(?idmsuxU-idmsuxU) | Nothing, but turns the given flags on / off for the rest of the pattern (see the flags table). |
(?idmsux-idmsux:X) | X as a non-capturing group with the given flags on / off. |
(?=X), (?!X) | X via zero-width positive / negative lookahead. |
(?<=X), (?<!X) | X via zero-width positive / negative lookbehind (Java requires a bounded length; JavaScript does not). |
(?>X) | X as an independent (atomic) non-capturing group. Approximated as a plain non-capturing group. |
Flags
| Flag | Inline | Effect |
|---|---|---|
CASE_INSENSITIVE | (?i) | Case-insensitive matching. By default only US-ASCII characters are folded; combine with UNICODE_CASE for other alphabets. |
MULTILINE | (?m) | ^ and $ match at the start and end of every line instead of the whole input. |
DOTALL | (?s) | . also matches line terminators. |
COMMENTS | (?x) | Whitespace in the pattern is ignored (inside character classes too) and # starts a comment that runs to the end of the line. Use \ or [ ] for a literal space. |
UNIX_LINES | (?d) | Only \n is recognised as a line terminator by ., ^ and $. |
CANON_EQ | — | Canonical equivalence: "é" matches "e" + combining acute accent. Not supported by JavaScript (ignored). |
LITERAL | — | The pattern is treated as a sequence of literal characters; metacharacters have no special meaning (like Pattern.quote()). |
UNICODE_CASE | (?u) | Case-insensitive matching uses the Unicode standard (requires CASE_INSENSITIVE). JavaScript's u flag. |
UNICODE_CHARACTER_CLASS | (?U) | The POSIX classes, \w, \d, \s and \b use Unicode definitions (Java 7+). Implies UNICODE_CASE. |
Java versus JavaScript regular expressions
| Feature | Java (java.util.regex) | JavaScript (RegExp) |
|---|---|---|
| Pattern in source code | String literal: backslashes must be doubled ("\\d+"), or use a text block. | Regex literal /\d+/g (no doubling) or new RegExp("\\d+", "g"). |
| Whole-input match | matcher.matches() or String.matches() — the entire input must match. | No equivalent: anchor the pattern with ^…$ (and no m flag) or use \A…\z semantics. |
| Find / iterate | matcher.find() in a loop, matcher.group(n). | regex.exec() in a loop with the g flag, or str.matchAll(). |
| Replacement references | $1, ${name}; \ escapes (\$ literal dollar). $ followed by a non-digit is an error. | $1, $<name>, $&, $`, $'; $$ literal dollar. Unknown references are left as text. |
Whitespace \s | ASCII only: [ \t\n\x0B\f\r] (Unicode with UNICODE_CHARACTER_CLASS). | Unicode: also NBSP, em space, BOM, line/paragraph separators… |
Dot . | Excludes \n \r \u0085 \u2028 \u2029; DOTALL includes them. | Excludes \n \r \u2028 \u2029; s flag includes them. |
$ without multiline | End of input or before a final line terminator. | Strictly the end of input. |
| Possessive quantifiers, atomic groups | Supported (a*+, (?>…)). | Not supported. |
POSIX classes \p{Alpha}… | Supported (ASCII). | Not supported; use ranges or Unicode properties with the u flag. |
| Unicode properties | Scripts, blocks, categories, binary properties. | Scripts, categories and binary properties with the u flag; no blocks. |
Character class intersection && | Supported. | Only with the newer v flag. |
| Lookbehind | Bounded length only. | Unbounded (Chrome, Firefox, Safari 16.4+). |
\Q…\E, \A, \Z, \z, \G, \h, \R | Supported. | Not supported. |
Inline flags (?i) | Anywhere in the pattern, scoped groups (?i:…). | Scoped groups only in recent engines; global flags otherwise. |
| Octal escapes | \0nn only. | \nn legacy form (not in u mode). |
| Named groups | Java 7+, names [a-zA-Z][a-zA-Z0-9]*, \k<name>. | ES2018+, any identifier, \k<name>. |