What Is a Regular Expression?
A regular expression (regex) is a sequence of characters defining a search pattern. Supported by virtually every programming language, regex is used for text search, data validation, string extraction, and find-and-replace operations.
A simple example:
Pattern: ^\d{4}-\d{2}-\d{2}$
Meaning: Match a date in YYYY-MM-DD format
Test: 2024-05-19 ✓ 2024-5-9 ✗ 2024/05/19 ✗
Regex Syntax Quick Reference
| Syntax | Meaning | Example |
|---|---|---|
. | Any single character (except newline) | a.c matches abc, a1c |
* | Previous element, 0 or more times | ab*c matches ac, abc, abbc |
+ | Previous element, 1 or more times | ab+c matches abc, abbc — not ac |
? | Previous element, 0 or 1 time | colou?r matches color and colour |
\d | Any digit [0-9] | \d{3} matches 3 digits |
\w | Word character [a-zA-Z0-9_] | \w+ matches a word |
\s | Whitespace (space, tab, etc.) | \s+ matches whitespace |
^ | Start of string | ^Hello matches strings starting with Hello |
$ | End of string | world$ matches strings ending with world |
[abc] | Character class | [aeiou] matches any vowel |
() | Capture group | (\d+)-(\d+) captures two number segments |
Common Regex Patterns
# Email validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
# US phone number
^(\+1)?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$
# URL
https?://[^\s/$.?#].[^\s]*
# IP address
\b(?:\d{1,3}\.){3}\d{1,3}\b
# Strong password (8+ chars, upper, lower, digit)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$
# HTML tags
<[^>]+>