BASICS ------ ^ Match the beginning of the string $ Match the end of the string . Match any character except newline \w Match any alphanumeric character \s Match any whitespace character \d Match any digit \b Match the beginning or end of a word REPITION -------- * Repeat any number of times + Repeat one or more times ? Repeat zero or one time {n} Repeat n times {n,m} Repeat at least n, but no more than m times {n,} Repeat at least n times NEGATION -------- \W Match any character that is NOT alphanumeric \S Match any character that is NOT whitespace \D Match any character that is NOT a digit \B Match a position that is NOT the beginning or end of a word [^x] Match any character that is NOT x [^abc] Match any character that is NOT one of the characters abc ALTERNATIVES ------------ | Pipe symbol separates alternatives. Evaluation is from left to right GROUPING -------- () A Group should be enclosed within brackets CAPTURES -------- (exp) Match exp and capture it in an automatically numbered group (?exp) Match exp and capture it in a group named name (?:exp) Match exp, but do not capture it LOOKAROUNDS ----------- (?=exp) Match any position preceding a suffix exp (?<=exp) Match any position following a prefix exp (?!exp) Match any position after which the suffix exp is not found (?).*(?=<\/\1>) Text between HTML tags This searches for an HTML tag using lookbehind and the corresponding closing tag using lookahead, thus capturing the intervening text but excluding both tags. GREEDY AND LAZY --------------- Repition quantifier default to "greedy" matching. Follow a quantifier with a ? mark to enforce "lazy" matching. *? Repeat any number of times, but as few as possible +? Repeat one or more times, but as few as possible ?? Repeat zero or one time, but as few as possible {n,m}? Repeat at least n, but no more than m times, but as few as possible {n,}? Repeat at least n times, but as few as possible