Regular Expressions: A Practical Guide with Examples

· 10 min read

Regex Basics

PatternMatchesExample
.Any character (except newline)a.c matches abc, a1c, a-c
\dDigit [0-9]\d{3} matches 123, 456
\wWord char [a-zA-Z0-9_]\w+ matches hello, user_1
\sWhitespace\s+ matches spaces, tabs
^Start of string/line^Hello matches Hello at start
$End of string/lineworld$ matches world at end
[abc]Character class[aeiou] matches any vowel
[^abc]Negated class[^0-9] matches non-digits
\bWord boundary\bcat\b matches cat not category

Quantifiers

QuantifierMeaningExample
*0 or moreab*c matches ac, abc, abbc
+1 or moreab+c matches abc, abbc (not ac)
?0 or 1colou?r matches color, colour
{n}Exactly n\d{4} matches 2026
{n,m}Between n and m\d{2,4} matches 12, 123, 1234
*?Lazy (non-greedy)<.*?> matches single HTML tags

Groups and Captures

// Capturing group
/(\d{4})-(\d{2})-(\d{2})/
// Matches: 2026-03-29
// Group 1: 2026, Group 2: 03, Group 3: 29

// Named group
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/

// Non-capturing group
/(?:https?|ftp):\/\//

// Alternation
/cat|dog|bird/

// Lookahead (positive)
/\d+(?= dollars)/  // matches 100 in "100 dollars"

// Lookbehind (positive)
/(?<=\$)\d+/  // matches 100 in "$100"

Common Patterns

PatternRegex
Email (simple)^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
URL^https?:\/\/[\w.-]+(?:\.[a-zA-Z]{2,})(?:\/\S*)?$
IPv4^(?:\d{1,3}\.){3}\d{1,3}$
Date (YYYY-MM-DD)^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
Phone (US)^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
Hex color^#(?:[0-9a-fA-F]{3}){1,2}$
Strong password^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Slug^[a-z0-9]+(?:-[a-z0-9]+)*$

Test your patterns with our Regex Tester.

Flags

FlagNameEffect
gGlobalFind all matches, not just the first
iCase-insensitive/hello/i matches Hello, HELLO
mMultiline^ and $ match line boundaries
sDotall. matches newlines too
uUnicodeCorrect handling of Unicode characters

Performance Tips

Frequently Asked Questions

What is the difference between * and + in regex?

* matches zero or more occurrences (the preceding element is optional). + matches one or more (at least one occurrence is required).

How do I match a literal dot?

Escape it with a backslash: \. matches a literal period. Without the backslash, . matches any character.

What is a non-greedy (lazy) match?

Add ? after a quantifier to make it lazy: .*? matches as few characters as possible, while .* matches as many as possible.

Should I use regex to parse HTML?

No. HTML is not a regular language. Use a proper HTML parser (DOMParser, cheerio, BeautifulSoup). Regex is fine for simple pattern extraction but fails on nested structures.

Related Tools

Regex Tester Diff Checker URL Encoder