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/lineCharacter Classes
[abc]
Any of a, b, or c[^abc]
Not a, b, or c[a-z]
Range from a to z\\d
Digit [0-9]\\D
Non-digit [^0-9]\\w
Word character [a-zA-Z0-9_]\\W
Non-word character\\s
Whitespace\\S
Non-whitespaceQuantifiers
*
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 1Groups and Capturing
(pattern)
Capturing group(?:pattern)
Non-capturing group(?<name>pattern)
Named capturing group\\1
Backreference to group 1\\k<name>
Named backreference(?>pattern)
Independent non-capturing groupLookarounds
(?=pattern)
Positive lookahead(?!pattern)
Negative lookahead(?<=pattern)
Positive lookbehind(?<!pattern)
Negative lookbehindFlags (Pattern class)
Pattern.CASE_INSENSITIVE
Case insensitive matchingPattern.MULTILINE
^ and $ match line breaksPattern.DOTALL
Dot matches newlinePattern.UNICODE_CASE
Unicode case foldingPattern.CANON_EQ
Canonical equivalencePattern.COMMENTS
Allow comments and whitespaceUnicode
\\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 letterBoundaries
\\b
Word boundary\\B
Non-word boundary\\A
Start of input\\Z
End of input (before final terminator)\\z
End of input\\G
End of previous match