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\\dDigit [0-9]\\DNon-digit [^0-9]\\wWord character [a-zA-Z0-9_]\\WNon-word character\\sWhitespace\\SNon-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\\1Backreference 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_INSENSITIVECase insensitive matchingPattern.MULTILINE^ and $ match line breaksPattern.DOTALLDot matches newlinePattern.UNICODE_CASEUnicode case foldingPattern.CANON_EQCanonical equivalencePattern.COMMENTSAllow 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
\\bWord boundary\\BNon-word boundary\\AStart of input\\ZEnd of input (before final terminator)\\zEnd of input\\GEnd of previous match