Technology

Regular Expressions Cheat Sheet: Regex Tutorial and Reference

8 min read  ·  Updated 2025

Regular expressions (regex) are patterns used to match, search, and manipulate text. They are available in virtually every programming language and text editor. This guide covers everything from basic syntax to practical patterns with a complete reference cheat sheet.

Test all the patterns from this guide instantly with BrainBoost's Regex Tester.

Character Classes

PatternMatchesExample
.Any character except newlinec.t matches "cat", "cut", "c4t"
\dAny digit [0-9]\d+ matches "42", "007"
\DAny non-digit\D+ matches "hello", "abc"
\wWord character [a-zA-Z0-9_]\w+ matches "hello_world"
\WNon-word character\W+ matches "!@#"
\sWhitespace (space, tab, newline)\s+ matches spaces
\SNon-whitespace\S+ matches "hello"
[abc]Any of a, b, or c[aeiou] matches any vowel
[^abc]Any character NOT a, b, or c[^aeiou] matches consonants
[a-z]Any lowercase letter[a-f] matches a through f

Quantifiers

QuantifierMeaningExample
*0 or morebo* matches "b", "bo", "boo"
+1 or morebo+ matches "bo", "boo" (not "b")
?0 or 1 (optional)colou?r matches "color" and "colour"
{n}Exactly n times\d{4} matches exactly 4 digits
{n,}n or more times\d{2,} matches 2+ digits
{n,m}Between n and m times\d{2,4} matches 2 to 4 digits
*?Lazy: 0 or more (minimal)<.*?> matches one HTML tag at a time

Anchors and Boundaries

PatternMatches
^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\bWord boundary
\BNon-word boundary

Groups and Alternation

PatternMeaning
(abc)Capture group — match and remember "abc"
(?:abc)Non-capture group — match but don't remember
a|bAlternation — match "a" or "b"
\1Backreference to capture group 1

Common Real-World Patterns

PatternWhat it matches
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}Email address
https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-?=&#%]*URL (http/https)
\d{4}-\d{2}-\d{2}Date (YYYY-MM-DD)
\b(?:\d{1,3}\.){3}\d{1,3}\bIPv4 address
#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\bHex color code
^\+?[\d\s\-\(\)]{7,15}$Phone number
^(?=.*[A-Z])(?=.*\d).{8,}$Password (8+ chars, 1 uppercase, 1 digit)

Frequently Asked Questions

A regular expression (regex) is a sequence of characters that defines a search pattern. Used for finding, matching, and manipulating text in programming, text editors, and command-line tools.

The dot (.) matches any single character except a newline. The asterisk (*) means 'zero or more of the preceding element'. Together .* matches zero or more of any character.

g (global) — find all matches. i (case-insensitive) — match regardless of case. m (multiline) — ^ and $ match start/end of each line. s (dotall) — . matches newlines too.