Regular Expressions Guide for Beginners - Learn Regex in 10 Minutes
Regular Expressions Guide for Beginners - Learn Regex in 10 Minutes
Master the basics of regular expressions (regex) with practical examples. Learn pattern matching, character classes, quantifiers, and more.
March 12, 20262 min read
What Are Regular Expressions?
Regular expressions (regex) are patterns used to match character combinations in strings. They're incredibly powerful for text search, validation, and manipulation.
Basic Patterns
Literal Characters
The simplest regex is just a plain string: hello matches "hello" in any text.
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 | Any digit | \d{3} matches "123" |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ matches "hello" |
\s | Whitespace | \s+ matches spaces, tabs |
Quantifiers
| Quantifier | Meaning |
|---|---|
* | Zero or more |
+ | One or more |
? | Zero or one |
{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,}
Phone Number
\d{3}[-.\s]?\d{3}[-.\s]?\d{4}
URL Pattern
https?://[\w.-]+\.[a-zA-Z]{2,}[/\w.-]*
IP Address
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
Character Classes
[abc]- Matches a, b, or c[a-z]- Matches any lowercase letter[^abc]- Matches anything except a, b, or c[0-9]- Same as\d
Groups and Alternation
(abc)- Capturing group(?:abc)- Non-capturing groupa|b- Matches a or b
Test Your Regex
Practice makes perfect! Use our free Regex Tester to test your regular expressions in real-time with instant match highlighting.