
A Beginner's Guide to Regular Expressions — Learn Regex in 10 Minutes
📷 ThisIsEngineering / PexelsA Beginner's Guide to Regular Expressions — Learn Regex in 10 Minutes
Master the basics of regular expressions with practical examples. Learn pattern matching, character classes, quantifiers, and more.
What Are Regular Expressions?
A regular expression (regex) is a pattern that matches combinations of characters in a string. It is an extraordinarily powerful tool for searching text, validating input, and extracting data.
Basic Patterns
Literal Characters
The simplest regex is a plain string: hello matches the text "hello" anywhere in a larger string.
Special Characters (Metacharacters)
| Character | Meaning | Example |
|---|---|---|
. | Any single character | h.t matches "hat", "hot", "hit" |
^ | Start of string | ^Hello matches "Hello World" |
$ | End of string | world$ matches "hello world" |
\d | One digit | \d{3} matches "123" |
\w | Letter, digit, or underscore | \w+ matches "hello" |
\s | Whitespace character | \s+ matches spaces and tabs |
Quantifiers
| Quantifier | Meaning |
|---|---|
* | 0 or more times |
+ | 1 or more times |
? | 0 or 1 time |
{n} | Exactly n times |
{n,m} | Between n and m times |
Practical Examples
Email Validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
US Phone Number
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
URL Pattern
https?://[\w.-]+\.[a-zA-Z]{2,}[/\w.-]*
Try It Yourself
Practice is the best teacher. Test your regular expressions with real-time match highlighting in the free Regex Tester.