RegexDeveloper ToolsProgramming

Regular Expressions Cheat Sheet for Beginners

A practical guide to regular expressions (regex) with common patterns, examples, and tips for using regex effectively in your code.

ST
SmartToolsToday·March 10, 2026·8 min read
Ad · 728×90 Leaderboard

What are Regular Expressions?

Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They are supported in virtually every programming language and are incredibly powerful for text validation, searching, and transformation.

Basic Regex Syntax

PatternMeaningExample
.Any single characterc.t matches "cat", "cut", "c3t"
*0 or more of previousab* matches "a", "ab", "abb"
+1 or more of previousab+ matches "ab", "abb" (not "a")
?0 or 1 of previouscolou?r matches "color" and "colour"
^Start of string^Hello matches strings starting with "Hello"
$End of stringworld$ matches strings ending with "world"
[]Character class[aeiou] matches any vowel
[^]Negated character class[^0-9] matches any non-digit
{n,m}n to m repetitionsd{3,5} matches 3 to 5 digits
dAny digit (0-9)d+ matches one or more digits
wWord character [a-zA-Z0-9_]w+ matches a word
sWhitespaces+ matches spaces, tabs, newlines

Common Regex Patterns

  • Email: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}
  • URL: https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}
  • Phone (US): +?1?s?(?d{3})?[s.-]?d{3}[s.-]?d{4}
  • ZIP Code: d{5}(-d{4})?
  • Date (YYYY-MM-DD): d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]d|3[01])
  • IP Address: (?:d{1,3}.){3}d{1,3}

Regex Flags

  • g — Global: find all matches, not just the first
  • i — Case insensitive: treat uppercase and lowercase as equal
  • m — Multiline: ^ and $ match start/end of each line

Tips for Writing Regex

  1. Start simple and build up complexity gradually
  2. Always test with edge cases (empty strings, very long strings)
  3. Use named groups ((?<name>...)) for complex patterns
  4. Avoid greedy matching when possible — use *? and +?
  5. Comment complex regex using the x flag (where supported)

Practice your regex patterns with our free Regex Tester — it highlights all matches in real time as you type.

Ad · 728×90 Leaderboard