Regex Cheat Sheet: Master Patterns Fast (2025 Guide)

Regex cheat sheet is one of the most valuable tools for developers, sysadmins, and data analysts who work with text. Regular expressions (regex) allow you to match, search, and manipulate strings with powerful pattern-matching techniques.

Whether you are cleaning data, validating user inputs, or extracting information from logs, regex provides unmatched flexibility. This guide covers all essential regex syntax, operators, examples, and advanced tricks—so you can master patterns fast.

What is Regex?

Regex (short for regular expressions) is a sequence of characters that defines a search pattern. It is supported across most programming languages, including Python, JavaScript, Java, Perl, and many text editors like Vim, VS Code, and Sublime Text.

With regex, you can:

  • Validate formats (like email, phone numbers, or IP addresses).
  • Find and replace text in files.
  • Extract useful information from unstructured text.

In short, regex is a universal tool for text processing. This regex cheat sheet provides everything you need to get started.

Regex Basics Every Beginner Should Know

Learning regex can feel overwhelming at first, but once you know the building blocks, it becomes much easier.

Literal Characters

  • a, b, c → Match themselves. Example: /cat/ matches “cat”.
  • 123 → Matches “123” exactly.

Metacharacters

These characters have special meaning in regex:

  • . → Matches any single character.
  • ^ → Matches start of string.
  • $ → Matches end of string.
  • [] → Defines a set of characters.

Example: /^a.c$/ matches “abc”, “axc”, but not “abcc”.

Character Classes in Regex

Regex character classes help define groups of characters to match.

  • [abc] → Matches “a”, “b”, or “c”.
  • [^abc] → Matches any character except “a”, “b”, or “c”.
  • [0-9] → Matches digits 0–9.
  • [A-Z] → Matches uppercase letters.
  • [a-zA-Z0-9] → Matches letters and digits.

Shortcut classes:

  • \d → Digits (0–9).
  • \D → Non-digit characters.
  • \w → Word characters (letters, digits, underscore).
  • \W → Non-word characters.
  • \s → Whitespace (space, tab, newline).
  • \S → Non-whitespace.

Quantifiers in Regex

Quantifiers define how many times a character or group should appear.

  • * → 0 or more times.
  • + → 1 or more times.
  • ? → 0 or 1 time.
  • {n} → Exactly n times.
  • {n,} → At least n times.
  • {n,m} → Between n and m times.

Example:

  • /a*/ matches “a”, “aa”, “aaa”.
  • /\d{3}/ matches exactly 3 digits.

Anchors in Regex

Anchors don’t match characters but positions in the string.

  • ^ → Start of line/string.
  • $ → End of line/string.
  • \b → Word boundary.
  • \B → Non-word boundary.

Example:

  • /\bcat\b/ matches “cat” but not “category”.

Grouping and Capturing

Regex groups allow combining multiple tokens.

  • (abc) → Capturing group.
  • (?:abc) → Non-capturing group.
  • (?<name>abc) → Named capturing group.

Example:

  • Regex: (\d{3})-(\d{2})-(\d{4})
  • Match: “123-45-6789”
  • Captures: “123”, “45”, “6789”.

Regex Alternation

The pipe operator (|) works like OR in regex.

  • cat|dog → Matches either “cat” or “dog”.
  • (red|blue|green) → Matches one of the three colors.

This makes regex highly flexible for multiple options.

Escaping Special Characters

Special characters like ., ?, +, *, ^, $, and () must be escaped with a backslash if you want to match them literally.

Example:

  • Regex: /\$100/
  • Matches: “$100”.

Common Regex Examples

Here are some frequently used regex patterns you’ll find in practice:

  • Email validation:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
  • Phone number (US):
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
  • IP Address:
^(?:\d{1,3}\.){3}\d{1,3}$
  • Postal Code (5-digit):
^\d{5}$

These examples showcase the practical power of a regex cheat sheet for everyday development.

Regex Flags

Flags modify how regex behaves. Common ones include:

  • i → Case-insensitive match.
  • g → Global search (find all matches).
  • m → Multiline mode.
  • s → Dot matches newline.

Example:

  • /cat/i matches “cat” and “Cat”.
  • /dog/g finds all occurrences of “dog”.

Advanced Regex Features

Regex also supports advanced concepts for complex use cases.

Lookahead and Lookbehind

  • Positive lookahead: foo(?=bar) → Matches “foo” only if followed by “bar”.
  • Negative lookahead: foo(?!bar) → Matches “foo” not followed by “bar”.
  • Positive lookbehind: (?<=foo)bar → Matches “bar” only if preceded by “foo”.
  • Negative lookbehind: (?<!foo)bar → Matches “bar” not preceded by “foo”.

Backreferences

  • \1, \2, etc. → Refer to captured groups.
  • Example: (\w+)\s+\1 matches repeated words like “hello hello”.

Regex Cheat Sheet for Text Editors

Many text editors have built-in regex support:

  • VS Code: Use Ctrl + F, then enable regex search .*.
  • Vim/Neovim: Use :%s/pattern/replacement/g.
  • Sublime Text: Use Ctrl + H with regex toggle enabled.

This cheat sheet is especially useful for developers who edit large files daily.

Regex Performance Tips

  • Use specific patterns instead of greedy ones.
  • Avoid backtracking-heavy regex like nested quantifiers.
  • Precompile regex in programming languages for speed.

By optimizing regex usage, you avoid performance bottlenecks in large-scale text processing.

FAQ: Regex Cheat Sheet

1. What is a regex cheat sheet?

A regex cheat sheet is a quick reference guide that lists regex syntax, patterns, and examples to help developers match and manipulate text efficiently.

2. Is regex the same in all programming languages?

Regex syntax is mostly consistent, but some languages (like Python, Java, and JavaScript) have minor differences in supported features and flags.

3. How do I practice regex effectively?

You can practice regex on websites like regex101.com or by solving real-world text problems, such as validating inputs or searching through logs.

4. Why should I use a regex cheat sheet?

Because regex can be complex and hard to memorize, a cheat sheet saves time by providing ready-made patterns and explanations.

5. What are the most common regex use cases?

Common uses include validating emails, phone numbers, IP addresses, parsing log files, and performing advanced text replacements.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top