·12 min read

Regular Expressions Cheat Sheet and Tutorial

Regular expressions (regex) are one of the most powerful tools in a developer's toolkit. They let you search, match, and manipulate text with precision. This guide covers everything from basic syntax to advanced patterns, with practical examples you can use immediately.

What Are Regular Expressions?

A regular expression is a sequence of characters that defines a search pattern. Originally developed in the 1950s by mathematician Stephen Kleene, regex has become a fundamental feature of virtually every programming language and text editor. Whether you are validating email addresses, parsing log files, extracting data from HTML, or performing search-and-replace operations, regular expressions provide a concise and flexible way to match text patterns.

The syntax may look intimidating at first, but once you understand the building blocks, regex becomes an indispensable tool. Let us start with the fundamentals and work our way up to advanced techniques.

Regex Syntax Cheat Sheet

Character Classes

PatternDescription
.Any character except newline
\dAny digit (0-9)
\DAny non-digit
\wWord character (a-z, A-Z, 0-9, _)
\WAny non-word character
\sWhitespace (space, tab, newline)
[abc]Any character in the set (a, b, or c)
[^abc]Any character NOT in the set
[a-z]Any character in the range a to z

Quantifiers

PatternDescription
*Zero or more times
+One or more times
?Zero or one time (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times

Anchors and Boundaries

PatternDescription
^Start of string (or line with multiline flag)
$End of string (or line with multiline flag)
\bWord boundary

Groups, Lookaheads, and Advanced Features

Capturing Groups

Parentheses () create capturing groups that let you extract specific parts of a match. For example, to extract the area code from a US phone number:

Pattern: \((\d{3})\)\s(\d{3})-(\d{4})
Input:   (415) 555-1234
Group 1: 415
Group 2: 555
Group 3: 1234

Use (?:...) for non-capturing groups when you need grouping but do not need to extract the match. Named groups with (?<name>...) make your regex more readable:

/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
// match.groups.year => "2026"
// match.groups.month => "03"
// match.groups.day => "15"

Lookaheads and Lookbehinds

Lookaheads and lookbehinds (collectively called “lookarounds”) let you assert that a pattern exists before or after your match without including it in the result:

SyntaxTypeMeaning
(?=...)Positive lookaheadFollowed by ...
(?!...)Negative lookaheadNOT followed by ...
(?<=...)Positive lookbehindPreceded by ...
(?<!...)Negative lookbehindNOT preceded by ...

A practical example: match a price amount only if it is preceded by a dollar sign, but do not include the dollar sign in the match:

/(?<=\$)\d+\.\d{2}/g
Input:  "Price: $29.99, Tax: $3.50"
Matches: ["29.99", "3.50"]

Common Regex Patterns for Developers

Here are battle-tested patterns you can copy and use in your projects. Remember to adjust them based on your specific requirements.

Email Validation

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

Note: For production email validation, use a dedicated library. Email RFCs are complex.

URL Matching

/https?:\/\/[\w\-]+(\.[\w\-]+)+[\/\w\-._~:?#[\]@!$&'()*+,;=]*/gi

IP Address (IPv4)

/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/

Strong Password

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Requires: 8+ chars, uppercase, lowercase, digit, special character.

ISO Date Format

/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/

Hex Color Code

/^#(?:[0-9a-fA-F]{3}){1,2}$/

Regex Tips and Best Practices

  • Be specific over greedy. Use .*? (lazy) instead of .* (greedy) when you want the shortest possible match. Greedy quantifiers can cause unexpected behavior and performance issues (catastrophic backtracking).
  • Use flags wisely. The i flag enables case-insensitive matching. The g flag finds all matches (not just the first). The m flag makes ^ and $ match line boundaries.
  • Test incrementally. Build your regex pattern step by step, testing each addition. Start with a simple match and add complexity. Use an online regex tester to see matches highlighted in real time.
  • Document complex patterns. Use the x (verbose) flag in languages that support it (Python, Ruby, Perl) to add comments and whitespace to your patterns. In JavaScript, add a comment above the regex explaining what it matches.
  • Consider alternatives. For simple string operations, built-in string methods like includes(), startsWith(), and split() are often clearer and faster than regex. Use regex when you genuinely need pattern matching.
  • Escape special characters. If you are matching literal characters that have special meaning in regex (such as ., *, +, ?), prefix them with a backslash. A string escaping tool can help you handle this correctly.

Regex Across Programming Languages

While the core regex syntax is similar across languages, there are important differences in how you create and use regular expressions:

// JavaScript
const match = "hello world".match(/\b(\w+)\b/g)

# Python
import re
match = re.findall(r'\b(\w+)\b', "hello world")

// Go
re := regexp.MustCompile(`\b(\w+)\b`)
matches := re.FindAllString("hello world", -1)

// Java
Pattern p = Pattern.compile("\\b(\\w+)\\b");
Matcher m = p.matcher("hello world");

Note how JavaScript uses regex literals (/pattern/flags), Python uses raw strings (r'...'), and Java requires double-escaping backslashes. Understanding these differences is key when translating patterns between languages.

Related Tools

Practice and test your regular expressions with these free tools: