Regex Cheatsheet

The ultimate regular expression reference. Common patterns for validation and a complete syntax guide for everyday use.

Ready-to-Use Patterns

Email Address

Matches standard email formats like user@example.com.

^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$

Strong Password

Min 8 chars, at least 1 letter and 1 number.

^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$

IPv4 Address

Matches valid IPv4 addresses (0.0.0.0 to 255.255.255.255).

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

Date (YYYY-MM-DD)

Matches ISO 8601 dates (e.g., 2023-12-31).

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

Hex Color

Matches 3 or 6 digit hex codes, optional #.

^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

URL Slug

Lowercase letters, numbers, and hyphens only.

^[a-z0-9]+(?:-[a-z0-9]+)*$

Syntax Reference

Anchors

SymbolMeaning
^Start of string/line
$End of string/line
\bWord boundary

Quantifiers

SymbolMeaning
*0 or more
+1 or more
?0 or 1
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m

Character Classes

SymbolMeaning
.Any character (except newline)
\dDigit (0-9)
\wWord char (A-Z, a-z, 0-9, _)
\sWhitespace
\DNot a digit
\WNot a word char
\SNot whitespace

Sets & Ranges

SymbolMeaning
[abc]Any of a, b, or c
[^abc]None of a, b, or c
[a-z]Char in range a-z

Groups

SymbolMeaning
(...)Capture group
(?:...)Non-capturing group
(?=...)Positive lookahead
(?!...)Negative lookahead

# Master Regex with Common Patterns and Syntax

Regular Expressions (Regex) are an incredibly powerful tool for text processing, validation, and searching. However, their cryptic syntax can be difficult to remember.

This Regex Cheatsheet helps you quickly find the code you need. Use the Ready-to-Use Patterns for common tasks like validating emails, passwords, and dates. If you are building your own regex, consult theSyntax Reference for a clear explanation of anchors, quantifiers, and character classes.

Tips for Learning Regex

  • Start simple: Match literal characters first.
  • Use tools like our Regex Tester to experiment.
  • Break complex patterns down into smaller groups.