What Are Regular Expressions?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. They are built into virtually every programming language and are used for:
- Validation: Does this string match the pattern for a valid email address?
- Search: Find all phone numbers in this 10,000-word document.
- Replacement: Replace all occurrences of "colour" with "color".
- Extraction: Pull all URLs from a block of HTML.
- Splitting: Split a string at commas, but not commas inside quotes.
Core Metacharacters and What They Match

| Character | Matches | Example |
|---|---|---|
. | Any single character except newline | c.t matches "cat", "cot", "cut" |
d | Any digit (0–9) | ddd matches "123" |
w | Word character (A-Z, a-z, 0-9, _) | w+ matches "hello_world" |
s | Whitespace (space, tab, newline) | s+ matches any whitespace |
D | Non-digit | D+ matches "hello" |
W | Non-word character | W matches "!" or "," |
S | Non-whitespace |
Escaping: To match a literal metacharacter, escape with \. To match a literal ., use \.
Quantifiers: Repeating Patterns

| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more (greedy) | ab*c matches "ac", "abc", "abbc" |
+ | 1 or more (greedy) | ab+c matches "abc", "abbc" but not "ac" |
? | 0 or 1 (optional) | colou?r matches "color" and "colour" |
{n} | Exactly n times | d{4} matches exactly 4 digits |
{n,m} | Between n and m times | d{3,5} matches 3, 4, or 5 digits |
*? | 0 or more (lazy) | Matches as few as possible |
+? | 1 or more (lazy) | Matches as few as possible |
Character Classes and Anchors

Character classes [...]: Match any one character from a set.
[aeiou]— matches any vowel[A-Z]— any uppercase letter[0-9]— any digit (same asd)[^aeiou]— any character that is NOT a vowel (negation)
Anchors: Match positions, not characters.
^— matches start of string$— matches end of string— matches word boundary
Practical Regex: Email, Phone, URL Patterns


Basic email validation:
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$
US phone number (multiple formats):
^(\+1)?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
URL extraction from text:
https?:\/\/[\w\-]+(\.[\w\-]+)+[/#?]?[\w\-._~:/?#[\]@!$&'()*+,;=%]*
Test and debug all these patterns in real time using the free, client-side ToolDolfen Regex Tester with instant match highlighting.
Recommended Companion Guides
Explore complementary tutorials and utilities from this topic cluster to enhance your digital workflow: