Regular Expressions Reference Sheet

Pattern Description
. Matches any single character except newline
^ Matches the start of a line
$ Matches the end of a line
* Matches 0 or more repetitions of the preceding element
+ Matches 1 or more repetitions of the preceding element
? Makes the preceding element optional (matches 0 or 1 times)
{n} Matches exactly n repetitions of the preceding element
{n,} Matches n or more repetitions of the preceding element
{n,m} Matches between n and m repetitions
[abc] Character set: matches any one of the characters a, b, or c
[^abc] Negated character set: matches any character except a, b, or c
\d Matches any digit character (equivalent to [0-9])
\D Matches any non-digit character
\w Matches any word character (alphanumeric and underscore)
\W Matches any non-word character
\s Matches any whitespace character (spaces, tabs, line breaks)
\S Matches any non-whitespace character
(...) Capturing group: groups multiple tokens together and captures the match
(?:...) Non-capturing group: groups multiple tokens without capturing
| Alternation: matches either the expression before or after the | (OR operator)
\b Matches a word boundary (the position between a word character and a non-word character)
\B Matches where \b does not, i.e., not a word boundary
(?=...) Positive lookahead: matches a group after the main expression without including it in the result
(?!...) Negative lookahead: specifies a group that can’t match after the main expression
(?<=...) Positive lookbehind: matches a group before the main expression without including it in the result
(? Negative lookbehind: specifies a group that can’t match before the main expression
\ Escape character: used to escape special characters

Common Regular Expression Examples

Pattern Matches
^\d{5}$ Exactly 5 digits (e.g., US ZIP codes like 12345)
^\w+@\w+\.\w{2,}$ Simple email addresses (e.g., [email protected])
^(https?|ftp)://[^\s/$.?#].[^\s]*$ URLs starting with http://, https://, or ftp://
\b\d{3}-\d{2}-\d{4}\b Social Security numbers (e.g., 123-45-6789)
^\(\d{3}\) \d{3}-\d{4}$ US phone numbers in format (123) 456-7890
(?i)^[a-z]+$ One or more letters (case-insensitive)
\b\w{4}\b Words with exactly 4 letters
(\d{1,3}\.){3}\d{1,3} IP addresses (e.g., 192.168.1.1)
^[A-Fa-f0-9]{6}$ Hex color codes (without #, e.g., FFA07A)
^\d{4}-\d{2}-\d{2}$ Date in format YYYY-MM-DD (e.g., 2023-01-31)

Return to Home