Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

The Pattern class

Reference for java.util.regex.Pattern: compile a regular expression once, create matchers, use flags, split, quote literals and stream matches.

java.util.regex.Pattern is a compiled regular expression. Compiling parses the pattern text into an internal program; the resulting object is immutable and thread-safe, so you can keep it in a static final field and reuse it everywhere. A Pattern does not match anything on its own: you call matcher(text) to get a Matcher, which holds the matching state for one input.

String methods such as matches, replaceAll and split compile the pattern on every call. That is fine for one-off use. In loops or hot paths, compile once and reuse.

Java
private static final Pattern DATE = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");

Matcher m = DATE.matcher("due 2026-03-14");
if (m.find()) { System.out.println(m.group(1)); }

Try it

Java Compile once, match many times

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Streams, predicates, split and quote

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public final class Pattern implements Serializable
public static Pattern compile(String regex)
public static Pattern compile(String regex, int flags)
Member Description
compile(regex) / compile(regex, flags) Creates a pattern. Flags are combined with | (see pattern flags).
matcher(CharSequence) New Matcher for the input.
static matches(regex, input) Compile and test the whole input in one call.
static quote(String) Returns a pattern string that matches the text literally.
split(CharSequence), split(CharSequence, int limit) Splits around matches.
splitAsStream(CharSequence) Same as a Stream<String> (Java 8).
asPredicate() Predicate that is true if the pattern is found anywhere (Java 8).
asMatchPredicate() Predicate that is true if the whole string matches (Java 11).
pattern(), toString(), flags() The source text and flags.
namedGroups() Map of group names to numbers (Java 20).

Throws: PatternSyntaxException (unchecked) from compile and the static helpers when the regex is invalid; NullPointerException for a null argument.

Usage notes

  • Compile once. Put frequently used patterns in static final fields. Compilation is much more expensive than a match.
  • Thread safety. Pattern is safe to share; Matcher is not. Create a new matcher per thread and per input.
  • Escaping in strings. A regex backslash needs a second one in a Java string literal: "\\d+". A text block does not remove this requirement, though it avoids escaping quotes. The regex dot, plus, question mark and parentheses are special; use Pattern.quote or \\Q...\\E for user-supplied literals.
  • matches vs find. matcher.matches() requires the whole input to match, lookingAt() anchors at the start only, find() searches anywhere. asPredicate() uses find, asMatchPredicate() uses matches.
  • Trailing empty strings. split drops them unless the limit is negative; see Pattern.split().
  • Catastrophic backtracking can freeze a thread on hostile input; see regex pitfalls.
  • Unicode. By default \w, \d and \b are ASCII-based; add UNICODE_CHARACTER_CLASS for full Unicode meaning.