Regular Expressions for Beginners: Learn Regex in 10 Minutes
Learn the basics of regular expressions (regex) with practical examples. Master pattern matching for email validation, phone numbers, URLs, and more.
What is Regex?
Regular expressions (regex) are patterns used to match text. They're supported in virtually every programming language and text editor. Once you learn regex, you'll wonder how you ever lived without it.
Test your patterns in real-time with our Regex Tester.
Basic Building Blocks
Literal Characters
The simplest regex is just text: hello matches "hello" in any string.
Special Characters (Metacharacters)
These characters have special meaning in regex:
| Character | Meaning | Example |
|---|
. | Any character | h.t matches "hat", "hot", "hit" |
|---|---|---|
^ | Start of string | ^Hello matches "Hello world" |
$ | End of string | world$ matches "Hello world" |
* | 0 or more | ab*c matches "ac", "abc", "abbc" |
+ | 1 or more | ab+c matches "abc", "abbc" but not "ac" |
? | 0 or 1 | colou?r matches "color" and "colour" |
\ | Escape | \. matches a literal period |
Character Classes
| Pattern | Matches |
|---|
[abc] | a, b, or c |
|---|---|
[a-z] | any lowercase letter |
[A-Z] | any uppercase letter |
[0-9] | any digit |
[^abc] | anything except a, b, c |
Shorthand Classes
| Shorthand | Equivalent | Meaning |
|---|
\d | [0-9] | digit |
|---|---|---|
\w | [a-zA-Z0-9_] | word character |
\s | [ \t\n\r] | whitespace |
\D | [^0-9] | non-digit |
\W | [^a-zA-Z0-9_] | non-word |
Quantifiers
| Quantifier | Meaning |
|---|
{3} | Exactly 3 times |
|---|---|
{2,5} | Between 2 and 5 times |
{3,} | 3 or more times |
Practical Examples
Email Validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Phone Number (US)
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
Matches: (555) 123-4567, 555-123-4567, 555.123.4567
URL
https?://[\w.-]+(?:\.[a-z]{2,})+[/\w.-]*
IP Address
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
Date (YYYY-MM-DD)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Groups and Capturing
Parentheses create groups:
(abc)- captures "abc" as a group(?:abc)- groups without capturing (non-capturing)(a|b)- matches "a" or "b"
Common Mistakes
1. Forgetting to escape special characters - . matches ANY character, use \. for literal period
2. Greedy matching - .* is greedy by default, use .*? for lazy matching
3. Not anchoring - without ^ and $, your pattern matches anywhere in the string
4. Over-engineering - start simple and add complexity as needed
Practice Your Regex
The best way to learn regex is by doing. Use our Regex Tester to practice patterns with real-time highlighting and match information.
Other useful text tools:
- Text Counter - count characters, words, and lines
- Case Converter - transform text case
- Text Diff - compare two texts side by side