Basics of Regular Expressions

. (dot)

Matches any character except newline.

* (asterisk)

A quantifier. Matches 0 or more of the previous character.

? (question mark)

A quantifier. Matches 0 or 1 of the previous character.

+ (plus sign)

A quantifier. Matches 1 or more of the previous character.

[a-z]

matches any character between a and z, inclusive.

[A-z]

Matches any character between A and z. Note that this includes several non-alphabetical characters, including braces. See this ascii table for the full listing.

[a-zA-Z0-9]

Matches any upper or lowercase character or any number.

[^abc]

Matches any character except for a, b or c. The carat (^) at the beginning is what inverses the match.

^ (carat)

Match start of line.

$ (dollar sign)

Match end of line.

(backslash)

Escape any special characters using the backslash. For example, if you want to match a literal dollar sign ($) instead of the end of a line, use \$.

\s

Matches whitespace. Specifically, it matches:
' ' - space
'  ' - tab
'\n' - line break

Match a Digit

\d

Match a 'Word' Character

\w

Negative Lookaheads and Lookbehinds

Suppose you want to match an "a" in the string, but only if it is not followed by "ditya". You can do this with negative lookaheads:

a(?!ditya)


Now suppose you want to find "watterson" in the string, but only when it is not preceded by "bill ". Use a negative lookbehind:
(?<!bill )watterson