You are on page 1of 1

For m ore

Regex cheat sheet vis it rebe e cheat s heets

Character classes Useful Java classes & methods Quantifiers
[abc] matches a or b, or c. PATTERN
[^abc] negation, matches everything except a, b, or c. A pattern is a compiler representation of a regular expression. Greedy Reluctant Possessive Description
[a-c] range, matches a or b, or c.
Pattern compile(String regex)
[a-c[f-h]] union, matches a, b, c, f, g, h. X? X?? X?+ X, once or not at all.
Compiles the given regular expression into a pattern.
[a-c&&[b-c]] intersection, matches b or c.
[a-c&&[^b-c]] subtraction, matches a. X* X*? X*+ X, zero or more times.
Pattern compile(String regex, int flags)
Compiles the given regular expression into a pattern
X+ X+? X++ X, one or more times.
Predefined character classes with the given flags.

. Any character. boolean matches(String regex) X{n} X{n}? X{n}+ X, exactly n times.
\d A digit: [0-9] Tells whether or not this string matches the given
\D A non-digit: [^0-9] regular expression.
\s A whitespace character: [ \t\n\x0B\f\r] X{n,} X{n,}? X{n,}+ X, at least n times.
\S A non-whitespace character: [^\s] String[] split(CharSequence input)
\w A word character: [a-zA-Z_0-9] Splits the given input sequence around matches of X, at least n but
X{n,m} X{n,m}? X{n,m}+
\W A non-word character: [^\w] this pattern. not more than m times.

String quote(String s) Greedy - matches the longest matching group.
Boundary matches Returns a literal pattern String for the specified String. Reluctant - matches the shortest group.
^ The beginning of a line. Possessive - longest match or bust (no backoff).
$ The end of a line. Predicate<String> asPredicate()
\b A word boundary. Creates a predicate which can be used to match a string.
\B A non-word boundary. Groups & backreferences
\A The beginning of the input. MATCHER
\G The end of the previous match. An engine that performs match operations on a character A group is a captured subsequence of characters which may
\Z The end of the input but for the final terminator, if any. sequence by interpreting a Pattern. be used later in the expression with a backreference.
\z The end of the input. boolean matches()
(...) - defines a group.
Attempts to match the entire region against the pattern.
\N - refers to a matched group.
Pattern flags boolean find()
(\d\d) - a group of two digits.
Pattern.CASE_INSENSITIVE - enables case-insensitive Attempts to find the next subsequence of the input
(\d\d)/\1- two digits repeated twice.
matching. sequence that matches the pattern.
\1 - refers to the matched group.
Pattern.COMMENTS - whitespace and comments starting
with # are ignored until the end of a line. int start()
Pattern.MULTILINE - one expression can match
multiple lines.
Returns the start index of the previous match.
Logical operations
Pattern.UNIX_LINES - only the '\n' line terminator int end() XY X then Y.
is recognized in the behavior of ., ^, and $. Returns the offset after the last character matched. X|Y X or Y.