Regex Cheat Sheet
Interactive regex reference with categories: anchors, quantifiers, groups, character classes, lookahead/lookbehind. Examples for each pattern. Built-in regex tester.
Regex Tester
| Pattern | Description | Example | Matches | |
|---|---|---|---|---|
^ | Start of string (or line with m flag) | ^Hello | "Hello world" -> "Hello" | | |
$ | End of string (or line with m flag) | world$ | "Hello world" -> "world" | | |
\b | Word boundary | \bcat\b | "a cat sat" -> "cat" | | |
\B | Non-word boundary | \Bcat\B | "concatenate" -> "cat" | | |
* | Zero or more (greedy) | a* | "aaa" -> "aaa", "" -> "" | | |
+ | One or more (greedy) | a+ | "aaa" -> "aaa" | | |
? | Zero or one (optional) | colou?r | "color" and "colour" | | |
{n} | Exactly n times | a{3} | "aaa" -> "aaa" | | |
{n,} | n or more times | a{2,} | "aa", "aaa", "aaaa"... | | |
{n,m} | Between n and m times | a{2,4} | "aa", "aaa", "aaaa" | | |
*? | Zero or more (lazy) | a*? | Matches as few as possible | | |
+? | One or more (lazy) | a+? | Matches as few as possible | | |
?? | Zero or one (lazy) | a?? | Prefers zero | | |
. | Any character except newline | c.t | "cat", "cut", "c1t" | | |
[abc] | Any of a, b, or c | [aeiou] | Any vowel | | |
[^abc] | Not a, b, or c | [^0-9] | Any non-digit | | |
[a-z] | Any char from a to z | [a-zA-Z] | Any letter | | |
\d | Any digit [0-9] | \d{3} | "123", "456" | | |
\D | Any non-digit [^0-9] | \D+ | "abc", "hello" | | |
\w | Word character [a-zA-Z0-9_] | \w+ | "hello", "var_1" | | |
\W | Non-word character | \W+ | " ", "!@#" | | |
\s | Whitespace [ \t\n\r\f] | \s+ | " ", "\t", "\n" | | |
\S | Non-whitespace | \S+ | "hello", "123" | | |
(abc) | Capturing group | (\d{3})-(\d{4}) | Captures area code and number | | |
(?:abc) | Non-capturing group | (?:ab)+ | "abab" without capturing | | |
(?<name>abc) | Named capturing group | (?<year>\d{4}) | Captures with name "year" | | |
\1 | Backreference to group 1 | (\w+)\s+\1 | "the the" (repeated word) | | |
(a|b) | Alternation (or) | (cat|dog) | "cat" or "dog" | | |
(?=abc) | Positive lookahead | \d+(?= dollars) | "100" in "100 dollars" | | |
(?!abc) | Negative lookahead | \d+(?! dollars) | "100" NOT followed by " dollars" | | |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ | "100" in "$100" | | |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ | "100" NOT preceded by "$" | | |
\n | Newline | line1\nline2 | Matches newline character | | |
\t | Tab | col1\tcol2 | Matches tab character | | |
\r | Carriage return | \r\n | Windows line ending | | |
\\ | Literal backslash | C:\\Users | "C:\Users" | | |
\. | Literal dot | file\.txt | "file.txt" | | |
g | Global - find all matches | /cat/g | All occurrences of cat | |
i | Case insensitive | /hello/i | "Hello", "HELLO", "hello" | |
m | Multiline - ^ and $ match per line | /^start/m | Start of each line | |
s | Dotall - dot matches newline | /a.b/s | "a\nb" (across lines) | |
u | Unicode support | /\u{1F600}/u | Unicode emoji matching | |
Email | Email address pattern | [\w.-]+@[\w.-]+\.\w{2,} | "user@example.com" | |
URL | URL pattern | https?://[\w.-]+(?:/[\w.-]*)* | "https://example.com/path" | |
IP Address | IPv4 address | \d{1,3}(?:\.\d{1,3}){3} | "192.168.1.1" | |
Phone | US phone number | \(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} | "(555) 123-4567" | |
Date | Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} | "2024-01-15" | |
Hex Color | CSS hex color | #(?:[0-9a-fA-F]{3}){1,2} | "#FFF", "#3B82F6" | |
HTML Tag | HTML opening tag | <([a-z][a-z0-9]*)\b[^>]*> | "<div class=\"x\">" | |
Integer | Signed integer | -?\d+ | "42", "-7", "0" | |
Decimal | Decimal number | -?\d+\.?\d* | "3.14", "-2.5", "42" |
About Regular Expressions
Regular expressions (regex) are powerful patterns used for string matching, searching, and manipulation. This cheat sheet covers all major regex syntax with examples and a built-in tester.
Features
- Comprehensive regex reference organized by category
- Examples and match descriptions for every pattern
- Built-in regex tester with highlighting
- Common real-world patterns (email, URL, phone, etc.)
- Click any pattern to test it instantly
Some links on this page are affiliate links. If you click and make a purchase, we may earn a commission at no extra cost to you.
Recommended Products
AdAffiliate Disclosure: As an Amazon Associate, ToolBird earns from qualifying purchases. Links above are affiliate links — if you buy through them, we may earn a small commission at no extra cost to you.
Disclaimer: This tool is provided as-is for informational and educational purposes only.