Regular Expressions for Beginners: Find and Replace Text Like a Pro
Learn how to use regular expressions (regex) to find, match, and replace text patterns. Simple examples that anyone can understand and use right away.
What Are Regular Expressions?
Regular expressions (often called "regex") are like a super-powered find and replace tool. Instead of looking for exact words, they can find patterns in text.
💡 Think of It This Way
Imagine you want to find all phone numbers in a document. Instead of searching for each specific number, regex lets you search for "any pattern that looks like a phone number" - like XXX-XXX-XXXX.
Why Learn Regex?
🔍 Find Patterns
Find all email addresses, phone numbers, or dates in text files quickly.
🔄 Replace Text
Change formatting or fix common mistakes across hundreds of files at once.
✅ Validate Input
Check if users entered valid email addresses, passwords, or other data.
📊 Extract Data
Pull specific information from logs, reports, or any structured text.
Basic Regex Concepts
Let's start with the simple building blocks of regular expressions.
Literal Characters
The simplest regex just matches exact letters or numbers.
Pattern: cat
Finds the word "cat" exactly
I have a cat
The cat is sleeping
Special Characters (Metacharacters)
These characters have special meanings in regex:
Character | What It Does | Example | Matches |
---|---|---|---|
. |
Any single character | c.t |
cat, cot, cut, c5t |
* |
Zero or more of previous | ca*t |
ct, cat, caat, caaat |
+ |
One or more of previous | ca+t |
cat, caat, caaat |
? |
Zero or one of previous | ca?t |
ct, cat |
^ |
Start of line | ^Hello |
Hello at line start |
$ |
End of line | bye$ |
bye at line end |
Character Classes - Finding Groups of Characters
Character classes let you match any character from a specific group.
Common Character Classes
Digits and Numbers
[0-9] |
Any digit |
\d |
Same as [0-9] |
\d+ |
One or more digits |
Letters and Words
[a-z] |
Lowercase letters |
[A-Z] |
Uppercase letters |
\w |
Letters, digits, underscore |
Practice Examples
Find All Numbers
Pattern: \d+
Matches: 5, 12
Find Three-Letter Words
Pattern: \b\w{3}\b
Matches: The, big, red, car
Real-World Examples You Can Use Today
Here are practical regex patterns for common tasks:
Email Addresses
Simple Email Pattern
\w+@\w+\.\w+
Matches:
- john@email.com
- user@site.org
- admin@company.net
How It Works:
\w+
- one or more word characters@
- literal @ symbol\w+
- domain name\.
- literal dot\w+
- domain extension
Phone Numbers
US Phone Number Pattern
\d{3}-\d{3}-\d{4}
Matches:
- 555-123-4567
- 800-555-1234
- 123-456-7890
How It Works:
\d{3}
- exactly 3 digits-
- literal dash\d{3}
- exactly 3 digits-
- literal dash\d{4}
- exactly 4 digits
Dates
Date Pattern (MM/DD/YYYY)
\d{1,2}/\d{1,2}/\d{4}
Matches:
- 12/25/2023
- 1/1/2024
- 06/15/2023
How It Works:
\d{1,2}
- 1 or 2 digits (month)/
- literal slash\d{1,2}
- 1 or 2 digits (day)/
- literal slash\d{4}
- exactly 4 digits (year)
Where to Use Regex
You can use regular expressions in many different tools and programs:
Text Editors
- Visual Studio Code
- Notepad++
- Sublime Text
- Atom
How: Use Ctrl+H for find and replace, then enable "regex" option.
Programming Languages
- JavaScript
- Python
- PHP
- Java
How: Use built-in regex functions like match(), replace(), or search().
Quick Example in Different Tools
Task: Replace all dates from MM/DD/YYYY to DD-MM-YYYY
Find Pattern:
(\d{1,2})/(\d{1,2})/(\d{4})
Replace With:
$2-$1-$3
After: "Meeting on 25-12-2023"
Handy Regex Cheat Sheet
Save these patterns for quick reference:
Text Patterns
\w+ |
Any word |
\s+ |
Whitespace |
\S+ |
Non-whitespace |
.* |
Any text |
Useful Patterns
[A-Z]+ |
All caps words |
\$\d+ |
Dollar amounts |
#\w+ |
Hashtags |
@\w+ |
Social handles |
Practice Exercises
Try these exercises to improve your regex skills:
Exercise 1: Find URLs
Find all website URLs in text
Show Answer
Pattern: https?://\w+\.\w+
Exercise 2: Extract Usernames
Find all @usernames in social media text
Show Answer
Pattern: @\w+
Common Regex Mistakes to Avoid
❌ Common Mistakes
- Forgetting to escape special characters
- Making patterns too greedy (matching too much)
- Not testing patterns thoroughly
- Using complex patterns when simple ones work
- Forgetting case sensitivity
✅ Best Practices
- Start simple and build complexity gradually
- Test your patterns with sample data
- Use online regex testers for practice
- Comment complex patterns for later reference
- Consider using case-insensitive flags
Helpful Online Tools
These free tools can help you learn and test regex patterns:
Regex101
Test patterns and see matches highlighted
regex101.com
RegexPal
Simple regex testing with explanations
regexpal.com
Regexr
Visual regex builder with cheat sheet
regexr.com
Next Steps
Now that you know the basics, here's how to continue learning:
1. Practice Daily
Try to use regex in your daily work. Even simple find-and-replace tasks help build muscle memory.
2. Learn Advanced Features
Look into lookaheads, lookbehinds, and capturing groups when you're comfortable with basics.
3. Language-Specific Usage
Learn how to use regex in your favorite programming language or text editor.
4. Build a Pattern Library
Save useful patterns you create for future use. Document what each one does.
Start Using Regex Today
Regular expressions might seem scary at first, but they're incredibly useful once you get the hang of them. Start with simple patterns and gradually work your way up to more complex ones.
🎯 Remember
- Start with simple literal matches
- Learn one special character at a time
- Practice with real examples
- Use online tools to test your patterns
- Don't try to make perfect patterns right away
Ready to Practice Regex?
Use our text tools to practice finding and replacing patterns in your own content.