Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

Java regex syntax

Reference for the regular expression syntax supported by java.util.regex: character classes, quantifiers, anchors, groups, lookaround and escaping rules.

Java's regular expressions are written as ordinary strings and interpreted by java.util.regex.Pattern. The syntax is Perl-like: character classes, quantifiers, groups, alternation, anchors and lookaround. This page lists the constructs you will actually use, grouped by purpose, with a runnable table you can extend.

Two layers of escaping apply. The Java compiler first reads the string literal, so a regex \d must be written "\\d". Then the regex engine reads the result. A literal backslash to match is therefore "\\\\" in source.

Java
Pattern.compile("\\b\\d{3}-\\d{4}\\b")   // 555-0100 as a whole word

Try it

Java A tour of the constructs

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 Quantifiers and lookaround

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

Characters and classes

Construct Matches
x, \., \\ The character itself; escape specials with a backslash (regex level; double each backslash in Java source).
. Any character except a line terminator (any at all with DOTALL).
[abc], [^abc], [a-z], [a-c&&[^b]] Class, negated class, range, intersection.
\d \D Digit / non-digit.
\w \W Word character [A-Za-z0-9_] / opposite.
\s \S Whitespace / non-whitespace.
\h, \R, \X Horizontal space; any line break sequence; a grapheme cluster.
\p{Alpha}, \p{L}, \p{Lu}, \p{IsGreek}, \P{...} POSIX and Unicode categories, scripts; \P negates.

Quantifiers (each has a lazy form with ? and a possessive form with +)

Construct Meaning
*, +, ? 0 or more, 1 or more, 0 or 1.
, {n,}, {n,m} Exactly n, at least n, between n and m.
*?, +?, ??, {n,m}? Lazy: as few as possible.
*+, ++, ?+, {n,m}+ Possessive: never give characters back.

Anchors and boundaries

Construct Meaning
^, $ Start and end of input (of each line with MULTILINE).
\A, \z, \Z Start of input; end of input; end except a final terminator.
\b, \B Word boundary / not a boundary.
\G End of the previous match.

Groups and lookaround

Construct Meaning
(...), (?:...) Capturing group; non-capturing group.
(?<name>...), \k<name> Named group and its backreference.
\1 ... \9 Backreference to a numbered group.
(?=...), (?!...) Positive / negative lookahead.
(?<=...), (?<!...) Positive / negative lookbehind (bounded length).
(?>...) Atomic group.
(?i), (?s), (?m), (?x), (?u), (?-i), (?i:...) Inline flags, for the rest of the group or a sub-expression.
\Q...\E Everything between is literal.
a|b Alternation.

Usage notes

  • Greedy by default. <.+> swallows from the first < to the last >. Prefer a negated class (<[^>]+>) or a lazy quantifier.
  • Lookbehind needs a bounded length. (?<=a+) is rejected; (?<=a{1,3}) works.
  • ASCII by default. \w, \d, \s and \b ignore non-ASCII letters unless you set UNICODE_CHARACTER_CLASS (or (?U)). \p{L} is always Unicode-aware.
  • Inside [] only \, ^, -, [ and ] (and &&) are special, but escape - and ^ when in doubt.
  • Not supported: recursion, conditionals, and the \K reset available in some other engines. Java 9 added \R and \X.
  • Line terminators. ., ^ and $ know \n, \r, \r\n, \u0085, โ€จ, โ€ฉ unless UNIX_LINES is set.
  • Write complex patterns with COMMENTS and a text block so you can add whitespace and # comments.