Regex Practical Examples

Real-world regex use cases

Validation

^[a-zA-Z0-9]{6,}$
# username (6+ alphanumeric chars)
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$
# strong password

Extraction

\d+
# extract all numbers
#([a-fA-F0-9]{6})
# extract hex color codes

Replacement

(\w+)\s+(\w+) → $2 $1
# swap first two words

URL Parts

^(https?://)([^/]+)(/.*)?$
# captures protocol, domain, path

File Extensions

\.(jpg|jpeg|png|gif)$
# matches image files

Remove Tags

<[^>]*>
# matches HTML tags