Regex Cheat Sheet: Common Patterns for Everyday Use
Regex keeps slipping your mind? This guide breaks down the patterns you'll actually use — email, phone, URLs — with a free tester.
# Regex Cheat Sheet: Patterns You'll Actually Use
I avoided regex for years. Every time I needed to validate an email or parse a log line, I'd write 20 lines of string manipulation code instead of learning the syntax. Then I got paired with a senior dev who wrote ^[\w.-]+@[\w.-]+\.\w+$ in about 5 seconds and I felt stupid.
Here's what I wish someone had told me: you don't need to memorize regex. You just need a cheat sheet of patterns you'll actually use, and a tester to experiment with.
The Patterns I Use Weekly
Email validation:
`
^[\w.+-]+@[\w-]+\.[\w.-]+$
`
URL extraction:
`
https?://[\w.-]+(?:/[\w./?&=-]*)?
`
Phone number (US):
`
^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
`
Match all text between quotes:
`
"([^"]*)"
`
IP address:
`
^(?:\d{1,3}\.){3}\d{1,3}$
`
Date (YYYY-MM-DD):
`
^\d{4}-\d{2}-\d{2}$
`
The 10 Symbols That Cover 90% of Use Cases
| Symbol | Meaning |
|---|---|
. | Any character |
* | Zero or more |
+ | One or more |
? | Optional (zero or one) |
\d | Digit (0-9) |
\w | Word character (letter, digit, underscore) |
\s | Whitespace |
[] | Character set |
() | Capture group |
^ / $ | Start / end of string |
Flags Worth Knowing
g— Global (match all, not just first)i— Case-insensitivem— Multiline (^and$match line boundaries)
Testing Regex
Don't guess. Use our Regex Tester to paste your pattern and test text, see matches highlighted in real time. Free, no signup.
## Regex Cheat Sheet
Common patterns you will use repeatedly:
\d+— One or more digits\w+— One or more word characters (letters, digits, underscore)\s+— One or more whitespace characters[a-z]+— One or more lowercase letters[^a-z]— Any character that is not a lowercase letter^pattern— Match at the start of the stringpattern$— Match at the end of the string(\d{4})-(\d{2})-(\d{2})— Capture groups for a date(?:abc)— Non-capturing groupa|b— Match a OR b
Regex in Different Languages
Syntax is mostly consistent, but there are differences:
- JavaScript:
/pattern/flagsornew RegExp("pattern", "flags"). Flags:g(global),i(case-insensitive),m(multiline),s(dotall). - Python:
re.match(),re.search(),re.findall(),re.sub(). Use raw strings:r"d+". - Go:
regexp.Compile(),regexp.MustCompile(). Go uses RE2, which does not support backreferences. - Java:
Pattern.compile(),Matcherclass. Verbose syntax but full feature support. - grep:
grep -Efor extended regex,grep -Pfor PCRE (Perl-compatible).
Performance Tips
- Avoid catastrophic backtracking: Patterns like
(a+)+bcan cause exponential matching time on strings ofacharacters. Use atomic groups or possessive quantifiers when available. - Anchor your patterns:
^\d{4}$is faster than\d{4}because the anchors allow the engine to fail early. - Be specific:
[0-9]is faster than\din some engines because\dmay match Unicode digits. - Precompile: If you use a pattern in a loop, compile it once and reuse the compiled object.