Learn Guide

Regex Crash Course

Master pattern matching for search, validation, and parsing.

What is Regex?

A regular expression (regex) is a pattern that describes a set of strings. It is used for searching, validating, and extracting text across virtually every programming language.

Character Classes

Pattern Matches
[abc] a, b, or c
[a-z] Any lowercase letter
[^abc] Anything except a, b, c
\d Any digit (0–9)
\w Word character (letter, digit, _)
\s Whitespace (space, tab, newline)
. Any character except newline

Quantifiers

Pattern Meaning
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{3} Exactly 3
{2,5} Between 2 and 5

Add ? after a quantifier to make it lazy (match as few as possible): +?, *?

Anchors & Groups

Pattern Meaning
^ Start of string
$ End of string
\b Word boundary
(abc) Capturing group
(?:abc) Non-capturing group
a\|b a or b

Practical Examples

Email:       ^[\w.-]+@[\w.-]+\.\w{2,}$
Only digits: ^\d+$
Date:        ^\d{4}-\d{2}-\d{2}$
IPv4:        ^(\d{1,3}\.){3}\d{1,3}$
Hex color:   ^#[0-9a-fA-F]{6}$

Flags

Flag Effect
g Global — find all matches
i Case-insensitive
m Multiline — ^/$ match line start/end
s Dotall — . matches newline too