Regex Guide: Regular Expressions Cheat Sheet

Master regular expressions with our comprehensive guide and cheat sheet for pattern matching.

Table of Contents

What is Regular Expression?

A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used for string matching, search-and-replace operations, and input validation.

Basic Syntax

PatternDescriptionExample
.Any charactera.c matches "abc", "adc"
\dDigit (0-9)\d+ matches "123"
\wWord character\w+ matches "hello"
\sWhitespacea\sb matches "a b"
[abc]Character set[abc] matches "a", "b", or "c"
[^abc]Negated set[^abc] matches any except a,b,c

Quantifiers

QuantifierDescriptionExample
*0 or moreab* matches "a", "ab", "abb"
+1 or moreab+ matches "ab", "abb"
?0 or 1ab? matches "a", "ab"
{n}Exactly n timesa{3} matches "aaa"
{n,m}n to m timesa{2,4} matches "aa", "aaa", "aaaa"

Anchors and Boundaries

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

Regex Flags

  • g - Global match (find all matches)
  • i - Case insensitive
  • m - Multiline mode
  • s - Dot matches newline
  • u - Unicode mode

Common Examples

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}$

URL Matching

^https?://[^\s/$.?#].[^\s]*$

Try Our Regex Tester

Want to test your regex patterns? Use our free Regex Tester tool with live highlighting.