Regex Guide: Regular Expressions Cheat Sheet
Master regular expressions with our comprehensive guide and cheat sheet for pattern matching.
Table of Contents
What is Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used for string matching, search-and-replace operations, and input validation.
Basic Syntax
| Pattern | Description | Example |
|---|---|---|
. | Any character | a.c matches "abc", "adc" |
\d | Digit (0-9) | \d+ matches "123" |
\w | Word character | \w+ matches "hello" |
\s | Whitespace | a\sb matches "a b" |
[abc] | Character set | [abc] matches "a", "b", or "c" |
[^abc] | Negated set | [^abc] matches any except a,b,c |
Quantifiers
| Quantifier | Description | Example |
|---|---|---|
* | 0 or more | ab* matches "a", "ab", "abb" |
+ | 1 or more | ab+ matches "ab", "abb" |
? | 0 or 1 | ab? matches "a", "ab" |
{n} | Exactly n times | a{3} matches "aaa" |
{n,m} | n to m times | a{2,4} matches "aa", "aaa", "aaaa" |
Anchors and Boundaries
^- Start of string/line$- End of string/line\b- Word boundary\B- Non-word boundary
Regex Flags
g- Global match (find all matches)i- Case insensitivem- Multiline modes- Dot matches newlineu- Unicode mode
Common Examples
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Phone Number (US)
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
URL Matching
^https?://[^\s/$.?#].[^\s]*$
Try Our Regex Tester
Want to test your regex patterns? Use our free Regex Tester tool with live highlighting.