Regular Expressions Cheat Sheet for Developers
Regular expressions (regex) are a powerful tool for pattern matching and text manipulation. They can feel intimidating at first, but once you learn the core syntax, they become an indispensable part of your toolkit. This cheat sheet covers the essentials you will use most often.
Basic Matching
.matches any single character except a newline.*matches zero or more of the preceding element.+matches one or more of the preceding element.?matches zero or one of the preceding element (makes it optional).{n}matches exactly n occurrences.{n,m}matches between n and m.
Character Classes
Character classes let you match specific sets of characters:
[abc]matches a, b, or c.[^abc]matches any character except a, b, or c.[a-z]matches any lowercase letter.[0-9]matches any digit.\dmatches any digit (same as[0-9]).\wmatches any word character (letters, digits, underscore).\smatches any whitespace character (space, tab, newline).- Uppercase versions (
\D,\W,\S) match the opposite.
Anchors
Anchors do not match characters. They match positions in the string:
^matches the start of the string (or line, with themflag).$matches the end of the string (or line).\bmatches a word boundary, the position between a word character and a non-word character.
Groups and Alternation
(abc)creates a capturing group. The matched text can be referenced later.(?:abc)creates a non-capturing group. It groups the pattern without saving the match.a|bmatches either a or b (alternation).(?<name>abc)creates a named capturing group, accessible by name in your code.
Lookaheads and Lookbehinds
Lookarounds let you assert what comes before or after a match without including it in the result:
(?=abc)positive lookahead: matches ifabcfollows.(?!abc)negative lookahead: matches ifabcdoes not follow.(?<=abc)positive lookbehind: matches ifabcprecedes.(?<!abc)negative lookbehind: matches ifabcdoes not precede.
Common Real-World Patterns
Here are patterns you will reach for often. Remember that production-grade validation (especially for emails) is more nuanced than a single regex.
- Simple email:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} - URL (http/https):
https?:\/\/[^\s/$.?#].[^\s]* - US phone number:
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} - IPv4 address:
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b - Hex color code:
#?[0-9a-fA-F]{3,8}
Tips for Readable Regex
- Use named groups.
(?<year>\d{4})is much clearer than(\\d{4})when you read the code later. - Use the verbose flag. In languages that support it (Python's
re.VERBOSE), you can add comments and whitespace to your pattern. - Break complex patterns into smaller parts. Build and test each piece independently, then combine them.
- Prefer specific over greedy. Use
.*?(lazy) instead of.*(greedy) when you want the shortest match. - Test thoroughly. Edge cases in regex are notoriously tricky. Always test with both matching and non-matching inputs.
Flags
Flags modify how the entire pattern behaves:
g(global): find all matches, not just the first one.i(case-insensitive): ignore case when matching.m(multiline): make^and$match the start and end of each line.s(dotAll): make.match newline characters too.
Try it yourself
Test your regex patterns with real-time matching, capture groups, and flag support. Runs entirely in your browser.
Open Regex Tester