Wednesday, September 14, 2011

Regular expression cheat sheet

.Any character
^Start of line
$End of line
\dDigit character
\DAny character except digits
\sWhitespace character
\SAny character except whitespace
\wWord character
\WAny character except word characters
\bWord boundary
(x|y)x or y
x*Zero or more occurrences of x
x+One or more occurrences of x
x?Zero or one occurrences of x
x{m,n}At least m and at most n occurrences of x
x{m}Exactly m occurrences of x
[a-f]Character class containing a,b,c,d,e,f
(?is:x)Switch mode when evaluating x. i turns on ignoreCase, s means single-line mode

  • Symbols tend to have the same first letter as what they represent. Uppercase symbols define the complement. 
  • Use grouping properly: ab+ matches abbbb but (ab)+ matches ababab

No comments:

Post a Comment