Regular Expression Reference

Java Regular Expression Reference

Standard Regex Meta-Characters

Character Matches
The backslash character (\)
Octal character value 0n
Hexidecimal character value 0xn
Hexidecimal character value 0xn...n
Tab character
Vertical tab character
Newline character
Carriage-return character
Form-feed character
Alert (bell) character
Escape character
Control character corresponding to X

Regex Predefined Character Classes

Character Class Matches
Any character (except \n depending on engine)
Any digit character (0-9)
Any non-digit character (not 0-9)
Any whitespace character
Any non-whitespace character
Any word character
Any non-word character
x, g, or n
Not x, g, or n
All uppercase and lowercase letters
x, g, n, a, e, or i (union)
x or n (intersection)
All lowercase letters but not b or c (subtraction)
All lowercase letters but not g, h, or i (subtraction)

Regex POSIX Character Classes

Java Pattern POSIX Pattern Matches
All lowercase letters [a-z]
All uppercase letters [A-Z]
All ASCII characters
All uppercase and lowercase letters [A-Za-z]
All digits [0-9]
All letters and digits [A-Za-z0-9]
All punctuation [!"#$%&'()*+,\-./:;<=>?@\[\]^_`{|}~\\]
All letters, digits, and punctuation
All graph characters and space
Space or tab [\t ]
All control characters
All hexidecimal characters [A-Fa-f0-9]
All whitespace characters [ \t\n\v\f\r]

Regex Quantifiers

Symbol Greedy Usage Reluctant Usage Matches
Proceeding construct 0 or 1 time
Proceeding construct 0 or more times
Proceeding construct 1 or more times
Proceeding construct n times
Proceeding construct n or more times
Proceeding construct at least n but no more than m times

Regex Escaping & Quoting

Pattern Usage Meaning
When escaping does not produce a meta-character matches the following character, in the example, a dot
\Q and \E
\Qliteral text.\E
Uses the string literal between the start, \Q, and end, \E, ignoring special meanings of characters

Regex Logical Operators

Operator Usage Meaning
Matches A or B

Regex Groups & Back-references

Pattern Usage Meaning
Creates a capturing group, matching and capturing A
(?<name>To Match)
Creates a named capturing group with the given name
(?:To Match)+
Creates a non-capturing group
Matches the proceeding captured group n (ordered left to right)
Matches the proceeding captured group with the given name