Pattern
Use Pattern.compile(regex) to validate and compile an expression. Invalid syntax throws PatternSyntaxException.
Java regular expressions
Test a pattern against sample text, inspect every match, and generate Java code using Pattern and Matcher.
Enter a regex pattern, test it against sample text, and copy the generated Java Pattern and Matcher code.
Highlighted result
Matches found
Backslashes are escaped for a Java string literal, and selected flags are added to Pattern.compile().
import java.util.regex.Matcher;
import java.util.regex.Pattern;
String input = "Java is a programming language. JavaScript is not Java.";
Pattern pattern = Pattern.compile("\\bJava\\b");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.printf("Match '%s' at index %d%n", matcher.group(), matcher.start());
}Advertisement
Java regular expressions live in the java.util.regex package. A Pattern stores the compiled expression, and a Matcher applies that pattern to an input sequence. Compile once and reuse the pattern when the same expression runs repeatedly.
Use Pattern.compile(regex) to validate and compile an expression. Invalid syntax throws PatternSyntaxException.
Use matcher.find() to scan for occurrences, then read group(), start(), and end() for each match.
String.matches(), replaceAll(), and split() are convenient for small, one-off tasks. Reuse a compiled Pattern for repeated matching.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Pattern pattern = Pattern.compile("\\bJava\\b");
Matcher matcher = pattern.matcher("Java and JavaScript");
while (matcher.find()) {
System.out.println(matcher.group() + " at " + matcher.start());
}These are the tokens used most often in Java patterns. Test them in the tool above before placing the pattern inside a Java string literal.
| Token | Meaning in Java |
|---|---|
| . | Any character except a line terminator unless DOTALL is enabled |
| \d | A digit; use \p{IsDigit} or Unicode mode for broader Unicode behavior |
| \s | A whitespace character |
| \w | A word character |
| [abc] | One character from the set |
| [^abc] | One character not in the set |
| x+ | One or more repetitions of x |
| x* | Zero or more repetitions of x |
| x? | Zero or one occurrence of x |
| (group) | A numbered capturing group |
| (?<name>group) | A named capturing group in Java |
| ^ / $ | Start and end anchors; behavior changes with MULTILINE |
The regex engine and the Java compiler both interpret backslashes. A raw regex such as \d+ must therefore be written as "\\d+" in Java source. A word boundary \b becomes "\\b"; writing "\b" creates a backspace character instead.
\bJava\b"\\bJava\\b"Scans the input for the next matching subsequence. Use it for search and extraction.
Requires the pattern to match the entire input. Use it for validation.
Matches from the beginning of the input without requiring the entire input to match.
Use String.replaceAll() for a one-off replacement or Matcher.replaceAll() when you already have a compiled pattern. Replacement strings treat dollar signs and backslashes specially; use Matcher.quoteReplacement() for literal replacement text.
String input = "Order 123 costs 45 dollars";
String result = input.replaceAll("\\d+", "#");
System.out.println(result);
// Order # costs # dollars