Regular Expressions (Regex) Tip Sheet

Regular expressions can look like a secret code, but they are incredibly powerful for searching, extracting, and replacing text. This quick reference sheet breaks down the essential building blocks so you can start writing effective patterns immediately.

Core Syntax & Classes

Character classes act as shortcuts for common types of data, allowing you to match specific types of characters without listing them all out manually.

Syntax Matches Example Match
.Any single character (except a newline)a.c matches abc, a1c
\dAny digit (0-9)\d\d matches 42, 07
\DAny non-digit character\D matches A, @, space
\wAny word character (a-z, A-Z, 0-9, _)\w+ matches Hello_World
\sAny whitespace (space, tab, newline)\w\s\w matches a b
[abc]Any single character in the brackets[A-Z] matches B, X
[^abc]Any single character not in the brackets[^0-9] matches any non-number

Quantifiers & Boundaries

Quantifiers tell the regex engine how many times a pattern should occur. Boundaries anchor your search to specific parts of the text.

Syntax Description Example Match
*Zero or more occurrencesab*c matches ac, abc, abbbc
+One or more occurrencesab+c matches abc, abbc
?Zero or one occurrence (optional)colou?r matches color, colour
{n,m}Between n and m occurrences\d{2,4} matches 12, 123, 1234
^The start of a string/line^Hello matches Hello at the beginning
$The end of a string/lineend$ matches end at the tail
|Alternation (OR logical operator)cat|dog matches cat or dog

Pro Tips for Debugging

Note: Syntax can vary slightly depending on the regex flavor you are using (e.g., Python, JavaScript, PCRE), but these core concepts are virtually universal.