Java regular expressions

Java regex tester and practical syntax guide

Test a pattern against sample text, inspect every match, and generate Java code using Pattern and Matcher.

Interactive regex tester

Enter a regex pattern, test it against sample text, and copy the generated Java Pattern and Matcher code.

Pattern flags

Highlighted result

Java is a programming language. JavaScript is not Java.

Matches found

Match 1 at index 0: Java
Match 2 at index 50: Java

Java Pattern and Matcher code

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

How regular expressions work in Java

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.

Pattern

Use Pattern.compile(regex) to validate and compile an expression. Invalid syntax throws PatternSyntaxException.

Matcher

Use matcher.find() to scan for occurrences, then read group(), start(), and end() for each match.

String helpers

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());
}

Java regex syntax reference

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.

TokenMeaning in Java
.Any character except a line terminator unless DOTALL is enabled
\dA digit; use \p{IsDigit} or Unicode mode for broader Unicode behavior
\sA whitespace character
\wA 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

Java string escaping: why backslashes double

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.

Raw regex

\bJava\b

Java string literal

"\\bJava\\b"

find(), matches(), and lookingAt()

find()

Scans the input for the next matching subsequence. Use it for search and extraction.

matches()

Requires the pattern to match the entire input. Use it for validation.

lookingAt()

Matches from the beginning of the input without requiring the entire input to match.

Replacing text with a Java regex

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

Continue learning