Perl Regular Expression for .htaccess Cheat Sheet
Perl Regular Expression Cheat Sheet

This document presents a quick tabular summary of the regular expression (regexp) syntax in Perl, with examples.
The methods used here are also the methods you will need for manipulating your htaccess files.
Meta Characters
To present a meta character as a data character standing for itself, precede it with \ (e.g. \. matches the full stop character .).
| Char | Name (ISO 8859-1) | Unicode | HTML | Octal | Meaning |
|---|---|---|---|---|---|
| ^ | Circumflex accent | 005E | ^ | 136 | beginning of string |
| $ | Dollar sign | 0024 | $ | 44 | end of string |
| . | Full stop | 002E | . | 56 | any character except newline |
| * | Asterisk | 002A | * | 52 | match 0 or more times |
| + | Plus sign | 002B | + | 53 | match 1 or more times |
| ? | Question mark | 003F | ? | 77 | match 0 or 1 times; or: shortest match |
| | | Vertical line | 007C | | | 174 | alternative |
| ( ) | Left parenthesis Right parenthesis |
0028 0029 |
( ) |
50 51 |
grouping; "storing" |
| [ ] | Left square bracket Right square bracket |
005B 005D |
[ ] |
133 135 |
set of characters |
| { } | Left curly bracket Right square bracket |
007B 005D |
{ ] |
173 135 |
repetition modifier |
| | | Vertical line or bar | 007C | | | 174 | |
| \ | Reverse solidus or backslash | 005C | \ | 134 | quote or special and escape notation |
Repetition
| a* | zero or more occurrences of string, each of which matches pattern a |
| a+ | one or more occurrences of string, each of which matches pattern a |
| a? | zero or one occurrences of string, each of which matches pattern a (i.e., optional a) |
| a{m} | exactly m occurrences of string, each of which matches pattern a |
| a{m,} | at least m occurrences of string, each of which matches pattern a |
| a{m,n} | at least m but at most n occurrences of string, each of which matches pattern a |
| repetition? | same as any of the repetition expressions listed above but the shortest string matching the pattern is taken. The default is "greedy matching", which finds the longest match. (Perl version 5) |
Special notations with "Backslash"
| \t | tab |
| \n | newline |
| \r | return (CR) |
| \xhh | character with hex. code hh |
| \b | "word" boundary |
| \B | not a "word" boundary |
Special Matching Notations
| \w | matches any single character classified as a "word" character (alphanumeric or "_") |
| \W | matches any non-"word" character |
| \s | matches any whitespace character (space, tab, newline) |
| \S | matches any non-whitespace character |
| \d | matches any digit character, equiv. to [0-9] |
| \D | matches any non-digit character |
Character Sets (Character Class) Denoted By […]
| [characters] | matches any of the characters in the sequence |
| [x-y] | matches any of the characters x to y (inclusive) in the ASCII code |
| [\-] | matches the hyphen character |
| [\n] | matches the newline character, same for other single character denotations with \ (backslash) |
| [^something] | matches NOT something |
Examples
| abc | Find abc anywhere in the string. |
| ^abc | Find abc at the beginning of the string. |
| abc$ | Find abc at the end of the string. |
| a|b | Find either a OR b anywhere in the string. |
| ^abc|abc$ | Find abc at the beginning OR end of the string. |
| ab{2,4}c | Find an a followed by two, three or four b’s followed by a c. |
| ab{2,}c | Find an a followed by at least two b’s followed by a c . |
| ab+c | Find an a followed by one or more b’s followed by a c . |
| ab?c | Find an a followed by an optional b followed by a c. |
| a.c | Find an a followed by any single character except newline followed by a c. |
| a\.c | Matches exactly. |
| [abc] | Find either a OR b OR c. |
| \d{2} | Matches any two decimal digits, i.e. 42. |
| \w+ | Matches a "word", a nonempty sequence of alphanumeric characters and underscores. |
Many thanks to IT and communication (http://www.cs.tut.fi/~jkorpela/indexen.html) for the information contained within this cheat sheet.




