Regex Cheat Sheet

A comprehensive reference guide for Regular Expressions (regex) with patterns, examples, and detailed explanations. Perfect for developers, data analysts, and anyone working with text processing.

39 patterns found
Filter by category:

literal text

Basic

Match exact characters

Pattern:

text

Examples:

hello Matches the exact string 'hello'
123 Matches the exact number '123'
Hello World Matches 'Hello World' exactly (case sensitive)

Notes:

Most characters match themselves literally

. (dot)

Basic

Match any single character except newline

Pattern:

.

Examples:

h.llo Matches 'hello', 'hallo', 'h3llo', etc.
a.b Matches 'axb', 'a b', 'a5b', etc.
.... Matches any 4 characters

Notes:

Use \. to match a literal dot

\

Basic

Escape special characters

Pattern:

\character

Examples:

\. Matches a literal dot
\$ Matches a literal dollar sign
\\ Matches a literal backslash
\( Matches a literal opening parenthesis

Notes:

Backslash removes special meaning from metacharacters

[abc]

Character Classes

Match any one character from the set

Pattern:

[characters]

Examples:

[abc] Matches 'a', 'b', or 'c'
[0-9] Matches any digit from 0 to 9
[a-zA-Z] Matches any letter (upper or lowercase)
[aeiou] Matches any vowel

Notes:

Square brackets define a character class

[^abc]

Character Classes

Match any character NOT in the set

Pattern:

[^characters]

Examples:

[^abc] Matches any character except 'a', 'b', or 'c'
[^0-9] Matches any non-digit character
[^aeiou] Matches any consonant
[^\s] Matches any non-whitespace character

Notes:

Caret ^ at the beginning negates the character class

\d

Character Classes

Match any digit (0-9)

Pattern:

\d

Examples:

\d Matches '0', '1', '2', ..., '9'
\d\d Matches any two-digit number
\d+ Matches one or more digits

Notes:

Equivalent to [0-9]

\D

Character Classes

Match any non-digit character

Pattern:

\D

Examples:

\D Matches 'a', 'B', '!', ' ', etc.
\D+ Matches one or more non-digit characters
\d\D Matches a digit followed by a non-digit

Notes:

Equivalent to [^0-9]

\w

Character Classes

Match any word character (letters, digits, underscore)

Pattern:

\w

Examples:

\w Matches 'a', 'Z', '5', '_'
\w+ Matches one or more word characters
\w{3} Matches exactly 3 word characters

Notes:

Equivalent to [a-zA-Z0-9_]

\W

Character Classes

Match any non-word character

Pattern:

\W

Examples:

\W Matches '!', '@', ' ', '-', etc.
\w+\W Matches word characters followed by non-word character
\W+ Matches one or more non-word characters

Notes:

Equivalent to [^a-zA-Z0-9_]

\s

Character Classes

Match any whitespace character

Pattern:

\s

Examples:

\s Matches space, tab, newline, etc.
\s+ Matches one or more whitespace characters
\w+\s+\w+ Matches two words separated by whitespace

Notes:

Includes space, tab, newline, carriage return, form feed

\S

Character Classes

Match any non-whitespace character

Pattern:

\S

Examples:

\S Matches any visible character
\S+ Matches one or more non-whitespace characters
\S\s\S Matches non-space, space, non-space

Notes:

Any character except whitespace

+

Quantifiers

Match one or more of the preceding character

Pattern:

pattern+

Examples:

a+ Matches 'a', 'aa', 'aaa', etc.
\d+ Matches one or more digits
[a-z]+ Matches one or more lowercase letters

Notes:

Greedy quantifier - matches as many as possible

*

Quantifiers

Match zero or more of the preceding character

Pattern:

pattern*

Examples:

a* Matches '', 'a', 'aa', 'aaa', etc.
\d* Matches zero or more digits
.* Matches any characters (including empty string)

Notes:

Greedy quantifier - matches as many as possible

?

Quantifiers

Match zero or one of the preceding character

Pattern:

pattern?

Examples:

colou?r Matches 'color' or 'colour'
\d? Matches zero or one digit
https? Matches 'http' or 'https'

Notes:

Makes the preceding character optional

{n}

Quantifiers

Match exactly n occurrences

Pattern:

pattern{n}

Examples:

\d{3} Matches exactly 3 digits
a{5} Matches exactly 5 'a' characters
[0-9]{4} Matches exactly 4 digits (like a year)

Notes:

Exact repetition count

{n,m}

Quantifiers

Match between n and m occurrences

Pattern:

pattern{n,m}

Examples:

\d{2,4} Matches 2, 3, or 4 digits
a{1,3} Matches 'a', 'aa', or 'aaa'
[a-z]{3,6} Matches 3 to 6 lowercase letters

Notes:

Range of repetitions

{n,}

Quantifiers

Match n or more occurrences

Pattern:

pattern{n,}

Examples:

\d{3,} Matches 3 or more digits
a{2,} Matches 'aa', 'aaa', 'aaaa', etc.
\w{5,} Matches 5 or more word characters

Notes:

Minimum repetition count

^

Anchors

Match start of string/line

Pattern:

^pattern

Examples:

^Hello Matches 'Hello' only at the beginning
^\d+ Matches digits at the start of line
^[A-Z] Matches uppercase letter at beginning

Notes:

Anchors the match to the beginning of the string or line

$

Anchors

Match end of string/line

Pattern:

pattern$

Examples:

end$ Matches 'end' only at the end
\d+$ Matches digits at the end of line
\.$ Matches a period at the end

Notes:

Anchors the match to the end of the string or line

\b

Anchors

Match word boundary

Pattern:

\bpattern\b

Examples:

\bcat\b Matches 'cat' as a whole word, not 'category'
\btest Matches 'test' at the beginning of a word
ing\b Matches 'ing' at the end of a word

Notes:

Boundary between word (\w) and non-word (\W) characters

\B

Anchors

Match non-word boundary

Pattern:

\Bpattern\B

Examples:

\Bcat\B Matches 'cat' within a word, like in 'category'
\Btest Matches 'test' NOT at word boundary
ing\B Matches 'ing' NOT at word end

Notes:

NOT at word boundary - within a word

(pattern)

Groups

Create capturing group

Pattern:

(pattern)

Examples:

(abc)+ Matches 'abc', 'abcabc', 'abcabcabc', etc.
(\d{2})-(\d{2})-(\d{4}) Captures date parts: MM-DD-YYYY
(Mr|Mrs|Ms)\.\s+(\w+) Captures title and name

Notes:

Groups patterns and captures matched text for later use

(?:pattern)

Groups

Create non-capturing group

Pattern:

(?:pattern)

Examples:

(?:abc)+ Groups 'abc' but doesn't capture
(?:http|https):// Matches protocol without capturing
(?:Mr|Mrs|Ms)\.\s+(\w+) Groups title but only captures name

Notes:

Groups patterns but doesn't create a capture group

|

Groups

Match one pattern OR another

Pattern:

pattern1|pattern2

Examples:

cat|dog Matches either 'cat' or 'dog'
jpeg|jpg|png Matches any of these image extensions
(Mr|Mrs|Ms) Matches any of these titles

Notes:

Logical OR operator - matches either side

(?=pattern)

Lookaround

Positive lookahead - match if followed by pattern

Pattern:

pattern(?=lookahead)

Examples:

\w+(?=@) Matches username before @ in email
\d+(?=px) Matches numbers followed by 'px'
hello(?=\s+world) Matches 'hello' only if followed by ' world'

Notes:

Assertion - doesn't consume characters, just checks what follows

(?!pattern)

Lookaround

Negative lookahead - match if NOT followed by pattern

Pattern:

pattern(?!lookahead)

Examples:

\w+(?!@) Matches words NOT followed by @
\d+(?!px) Matches numbers NOT followed by 'px'
hello(?!\s+world) Matches 'hello' NOT followed by ' world'

Notes:

Assertion - matches only if NOT followed by the pattern

(?<=pattern)

Lookaround

Positive lookbehind - match if preceded by pattern

Pattern:

(?<=lookbehind)pattern

Examples:

(?<=@)\w+ Matches domain after @ in email
(?<=\$)\d+ Matches numbers preceded by $
(?<=hello\s)\w+ Matches word after 'hello '

Notes:

Assertion - checks what comes before the match

(?<!pattern)

Lookaround

Negative lookbehind - match if NOT preceded by pattern

Pattern:

(?<!lookbehind)pattern

Examples:

(?<!@)\w+ Matches words NOT preceded by @
(?<!\$)\d+ Matches numbers NOT preceded by $
(?<!hello\s)\w+ Matches word NOT after 'hello '

Notes:

Assertion - matches only if NOT preceded by the pattern

Email

Common Patterns

Match email addresses

Pattern:

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

Examples:

[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} Basic email pattern
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b Email with word boundaries

Notes:

Basic email validation - not RFC compliant but covers most cases

URL

Common Patterns

Match URLs

Pattern:

https?://[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

Examples:

https?://\w+\.\w+ Simple URL pattern
https?://[\w\-]+(\.[\w\-]+)+ More robust URL pattern

Notes:

Matches HTTP and HTTPS URLs

Phone Number

Common Patterns

Match phone numbers

Pattern:

\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}

Examples:

\d{3}-\d{3}-\d{4} Format: 123-456-7890
\(\d{3}\)\s\d{3}-\d{4} Format: (123) 456-7890
\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4} Flexible US phone format

Notes:

US phone number formats

IP Address

Common Patterns

Match IPv4 addresses

Pattern:

\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b

Examples:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} Simple IP pattern
\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b IP with word boundaries
\b(?:(?: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]?)\b Valid IP range (0-255)

Notes:

Basic IP pattern - doesn't validate ranges

Date

Common Patterns

Match date formats

Pattern:

\d{1,2}/\d{1,2}/\d{4}

Examples:

\d{1,2}/\d{1,2}/\d{4} MM/DD/YYYY or M/D/YYYY
\d{4}-\d{2}-\d{2} YYYY-MM-DD format
\b\d{1,2}[-/]\d{1,2}[-/]\d{4}\b Flexible date with - or /

Notes:

Common date formats - doesn't validate actual dates

Credit Card

Common Patterns

Match credit card numbers

Pattern:

\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{4}

Examples:

\d{16} 16 consecutive digits
\d{4}\s\d{4}\s\d{4}\s\d{4} Groups of 4 with spaces
\d{4}[\s\-]?\d{4}[\s\-]?\d{4}[\s\-]?\d{4} Flexible spacing/dashes

Notes:

Basic credit card format - doesn't validate card numbers

Hex Color

Common Patterns

Match hexadecimal color codes

Pattern:

#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3}

Examples:

#[0-9A-Fa-f]{6} 6-digit hex color: #FF0000
#[0-9A-Fa-f]{3} 3-digit hex color: #F00
#[0-9A-Fa-f]{6}|#[0-9A-Fa-f]{3} Both 3 and 6 digit hex colors

Notes:

Hexadecimal color codes with #

i

Flags

Case insensitive matching

Pattern:

/pattern/i

Examples:

/hello/i Matches 'hello', 'Hello', 'HELLO', etc.
/[a-z]+/i Matches letters regardless of case
/test/i Matches 'test', 'Test', 'TEST', etc.

Notes:

Ignores case when matching

g

Flags

Global matching (find all matches)

Pattern:

/pattern/g

Examples:

/\d+/g Finds all numbers in text
/cat/g Finds all occurrences of 'cat'
/[aeiou]/g Finds all vowels

Notes:

Without 'g', only first match is returned

m

Flags

Multiline mode (^ and $ match line breaks)

Pattern:

/pattern/m

Examples:

/^\w+/m Matches word at start of each line
/\w+$/m Matches word at end of each line
/^#.+$/m Matches lines starting with #

Notes:

Makes ^ and $ match start/end of lines, not just string

s

Flags

Dot matches newline characters

Pattern:

/pattern/s

Examples:

/.*/ vs /.*/s With 's', dot matches newlines too
/<.*>/s Matches tags across multiple lines

Notes:

Makes . match newline characters (\n)

Regex Pro Tips

πŸ§ͺ Test Your Patterns

Use our interactive regex tester to validate and experiment with patterns in real-time.

Try Regex Tester

Common Workflow

  1. Start with literal text: hello
  2. Add character classes: [a-z]
  3. Use quantifiers: +, *, ?
  4. Add anchors if needed: ^, $
  5. Test with different inputs
  6. Refine and optimize

Best Practices

  • Keep patterns simple and readable
  • Use (?:) for non-capturing groups
  • Escape special characters with \
  • Test edge cases and empty strings
  • Consider performance for large datasets
  • Document complex patterns with comments

Quick Reference

Basics:
. any char
\ escape
| or
Quantifiers:
* 0 or more
+ 1 or more
? 0 or 1
Character Classes:
\d digit
\w word char
\s whitespace
Anchors:
^ start
$ end
\b word boundary

πŸ“š What is Regex?

Regular Expressions (Regex) are powerful pattern-matching tools that allow you to search, match, and manipulate text using concise and flexible patterns. Originally developed in the 1950s and popularized in Unix tools like grep and sed, regex has become an essential skill for developers, data analysts, and anyone working with text processing. Think of regex as a sophisticated "find and replace" tool with superpowers.

πŸš€ Key Features

  • βœ“ Pattern matching: Find complex text patterns with simple expressions
  • βœ“ Text validation: Verify formats like emails, phone numbers, URLs
  • βœ“ Data extraction: Pull specific information from large text files
  • βœ“ Search & replace: Advanced find/replace operations with captured groups
  • βœ“ Cross-platform: Supported in virtually every programming language
  • βœ“ Concise syntax: Express complex patterns in short, readable forms

πŸ’‘ Why Use Regex?

  • β€’ Efficiency: Process thousands of lines of text in milliseconds
  • β€’ Precision: Match exact patterns with incredible specificity
  • β€’ Automation: Automate repetitive text processing tasks
  • β€’ Data cleaning: Standardize and sanitize messy data formats
  • β€’ Log analysis: Extract insights from server logs and reports
  • β€’ Form validation: Ensure user input meets required formats

🎯 Common Use Cases

Web Development

Email validation, URL parsing, form input sanitization, and user data validation

Data Analysis

Log file parsing, CSV data cleaning, extracting structured information from unstructured text

System Administration

Configuration file management, log monitoring, automated text processing scripts

πŸ” How Regex Works

Pattern Building Blocks

  • β€’ a-z Literal characters
  • β€’ [0-9] Character classes
  • β€’ +*? Quantifiers
  • β€’ ^$ Anchors
  • β€’ () Groups

Example Pattern

^\w+@[a-zA-Z_]+?\.[a-zA-Z]3$

Matches email addresses like: user@domain.com

πŸ—οΈ Regex Components

πŸ“ Literals

Regular characters that match themselves exactly

hello

πŸŽ›οΈ Metacharacters

Special characters with special meanings

. ^ $ * +

πŸ“Š Quantifiers

Specify how many times to match

+ * ? {n}

🎯 Anchors

Match positions, not characters

^ $ \b

πŸš€ Getting Started with Regex

1. Start Simple

Begin with literal text: cat matches "cat" β€’ Add a dot: c.t matches "cat", "cot", "cut"

2. Learn the Basics

Master character classes [0-9] β€’ Understand quantifiers +*? β€’ Practice with anchors ^$

3. Practice & Test

Use online testers β€’ Start with common patterns β€’ Build complexity gradually β€’ Test edge cases

βš–οΈ Regex Flavors

Popular Implementations

  • β€’ PCRE: Perl Compatible (PHP, Python)
  • β€’ ECMAScript: JavaScript standard
  • β€’ POSIX: Unix tools (grep, sed)
  • β€’ .NET: C#, VB.NET
  • β€’ Java: Built into Java platform

Key Differences

  • β€’ Lookahead/lookbehind support
  • β€’ Unicode property support
  • β€’ Escape sequence variations
  • β€’ Performance characteristics
  • β€’ Advanced features availability

Remember: Regex is like a superpowerβ€”it can save you hours of manual work, but it takes practice to master. Start with simple patterns and gradually build complexity. This cheat sheet is your companion on the journey to regex mastery! 🎯