Quick Summary: Regular expressions (regex) are powerful patterns used to match, search, and manipulate text. This guide covers everything from basic syntax to practical applications, helping you unlock the full potential of text processing.
What Are Regular Expressions?
Regular expressions are sequences of characters that define search patterns. They're used for:
- Validating user input (emails, phone numbers, passwords)
- Searching and replacing text in documents
- Extracting data from logs and files
- Parsing and processing text data
- URL routing in web applications
Basic Regex Syntax
Literal Characters
The simplest regex pattern matches exact characters:
| Pattern | Matches | Example |
|---|---|---|
| hello | The word "hello" | "say hello" ✓ |
| 123 | The digits "123" | "abc123def" ✓ |
| test | The word "test" | "testing" ✗ (partial match) |
Special Characters (Metacharacters)
Certain characters have special meanings in regex:
| Character | Meaning | Example |
|---|---|---|
| . | Any single character | c.t → cat, cut, cot |
| * | Zero or more of preceding | ab*c → ac, abc, abbc |
| + | One or more of preceding | ab+c → abc, abbc (not ac) |
| ? | Zero or one of preceding | colou?r → color, colour |
| ^ | Start of string | ^Hello → "Hello world" |
| $ | End of string | world$ → "Hello world" |
Character Classes
Character classes match specific sets of characters:
| Pattern | Matches | Example |
|---|---|---|
| [abc] | Any of a, b, or c | gr[ae]y → gray, grey |
| [a-z] | Any lowercase letter | [a-z]+ → "hello" |
| [0-9] | Any digit | [0-9]{3} → "123" |
| [^abc] | Not a, b, or c | [^aeiou] → consonants |
| \d | Any digit (same as [0-9]) | d{3} → "456" |
Quantifiers
Quantifiers specify how many times a pattern should match:
| Quantifier | Meaning | Example |
|---|---|---|
| {n} | Exactly n times | a{3} → "aaa" |
| {n,} | At least n times | a{2,} → "aa", "aaa" |
| {n,m} | Between n and m times | a{1,3} → "a", "aa", "aaa" |
Practical Examples
1. Email Validation
Pattern:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$Breakdown:
^- Start of string[a-zA-Z0-9._%+-]+- Username (letters, numbers, special chars)@- Literal @ symbol[a-zA-Z0-9.-]+- Domain name\.- Literal dot[a-zA-Z]{2,}- Top-level domain (2+ letters)$- End of string
2. Phone Number Validation
US Phone Number Pattern:
^(+1[-.s]?)?(?[0-9]{3})?[-.s]?[0-9]{3}[-.s]?[0-9]{4}$Matches: (123) 456-7890, 123-456-7890, 123.456.7890, +1 123 456 7890
3. URL Validation
Simple URL Pattern:
^https?://(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}.[a-zA-Z0-9()]{1,6}([-a-zA-Z0-9()@:%_+.~#?&//=]*)$Common Regex Patterns
| Use Case | Pattern |
|---|---|
| Password (8+ chars, mixed) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*d).{8,}$ |
| Hex Color Code | ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ |
| IP Address | ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ |
| Credit Card | ^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14})$ |
Testing Your Regex
Use our Regex Tester to:
- Test patterns against sample text in real-time
- See match highlights and captured groups
- Debug complex patterns step by step
- Save and share patterns with your team
Best Practices
1. Start Simple
Begin with basic patterns and gradually add complexity. Test each component before combining them.
2. Comment Complex Patterns
Many regex engines support comments. Document what each part does:
(
^\d{3} # Area code (3 digits)
[-\s]? # Optional separator
\d{3} # First part (3 digits)
[-\s]? # Optional separator
\d{4}$ # Last part (4 digits)
)3. Escape Special Characters
Remember to escape special characters when matching them literally:
- Use
\.to match a literal dot - Use
\*to match a literal asterisk - Use
\\to match a literal backslash
4. Consider Performance
Complex patterns with nested quantifiers can cause performance issues. Test with large inputs and consider using non-capturing groups (?:...)when you don't need to capture matches.
Conclusion
Regular expressions are an essential tool in every developer's toolkit. While they can seem intimidating at first, mastering the basics opens up powerful text processing capabilities. Start with simple patterns, practice regularly, and gradually tackle more complex use cases.
Practice Your Skills
Try our interactive regex tester to practice patterns and test your understanding.
Open Regex Tester →