Regular Expressions Cheat Sheet

Posted on

Expression Matches
* [abc] A single character: a, b, or c * [^abc] Any single character but a, b, or c * [a-z] Any character in the range a-z * [a-zA-Z] Any character in the range a-z or A-Z (any alphabetical character) * \s Any whitespace character [ \t\n\r\f\v] * \S Any non-whitespace character [^ \t\n\r\f\v] * \d Any digit [0-9] * \D Any non-digit [^0-9] * \w Any word character [a-zA-Z0-9] * \W Any non-word character [^a-zA-Z0-9] * \b A word boundary between \w and \W * \B A position that is not a word boundary * | Alternation: matches either the subexpression to the left or to the right * () Grouping: group all together for repetition operators * ^ Beginning of the string * $ End of the string

Repetition Operator Meaning
* {n,m} Match the previous item at least n times but no more than m times * {n,} Match the previous item n or more times * {n} Match exactly n occurrences of the previous item * ? Match 0 or 1 occurrences of the previous item {0,1} * + Match 1 or more occurrences of the previous item {1,} * * Match 0 or more occurrences of the previous item {0,}

Option Description
* g “Global” — find all matches in the string rather than just the first * i “case Insensitive” — ignore character case when matching * m “Multiline” — search over more than one line if the text contains line breaks

If you want to search for the following characters you must escape them with a \, ie \? or :
?,
,+,.,|,{,},\,[,],(,)

From this site.
Better tester