Regex Cheat Sheet - Java

A comprehensive guide to regular expressions with patterns, examples, and explanations

Basic Metacharacters

.Any character except newline
\\Escape character (double backslash in Java strings)
|Alternation (OR)
^Start of string/line
$End of string/line

Character Classes

[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range from a to z
\\dDigit [0-9]
\\DNon-digit [^0-9]
\\wWord character [a-zA-Z0-9_]
\\WNon-word character
\\sWhitespace
\\SNon-whitespace

Quantifiers

*0 or more
+1 or more
?0 or 1
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?Reluctant 0 or more
+?Reluctant 1 or more
??Reluctant 0 or 1
*+Possessive 0 or more
++Possessive 1 or more
?+Possessive 0 or 1

Groups and Capturing

(pattern)Capturing group
(?:pattern)Non-capturing group
(?<name>pattern)Named capturing group
\\1Backreference to group 1
\\k<name>Named backreference
(?>pattern)Independent non-capturing group

Lookarounds

(?=pattern)Positive lookahead
(?!pattern)Negative lookahead
(?<=pattern)Positive lookbehind
(?<!pattern)Negative lookbehind

Flags (Pattern class)

Pattern.CASE_INSENSITIVECase insensitive matching
Pattern.MULTILINE^ and $ match line breaks
Pattern.DOTALLDot matches newline
Pattern.UNICODE_CASEUnicode case folding
Pattern.CANON_EQCanonical equivalence
Pattern.COMMENTSAllow comments and whitespace

Unicode

\\p{L}Unicode letter
\\p{N}Unicode number
\\p{P}Unicode punctuation
\\p{IsLatin}Latin script block
\\p{InBasicLatin}Basic Latin Unicode block
\\P{L}Not Unicode letter

Boundaries

\\bWord boundary
\\BNon-word boundary
\\AStart of input
\\ZEnd of input (before final terminator)
\\zEnd of input
\\GEnd of previous match