Regex Tester: Master Regular Expressions with Examples
Learn regular expressions with our interactive regex tester. Complete guide with examples, patterns, and best practices for developers.
Regex Tester: Master Regular Expressions
Regular expressions (regex) are powerful tools for pattern matching and text manipulation. This comprehensive guide will help you master regex with practical examples.
What are Regular Expressions?
Regular expressions are sequences of characters that define search patterns. They're used for:
- Text validation (email, phone numbers, URLs)
- Search and replace operations
- Data extraction
- Input validation
- Log file parsing
Basic Regex Syntax
Literal Characters
cat
Matches the exact text "cat"
Character Classes
[abc] # Matches a, b, or c
[a-z] # Matches any lowercase letter
[A-Z] # Matches any uppercase letter
[0-9] # Matches any digit
[a-zA-Z] # Matches any letter
Predefined Character Classes
\d # Any digit [0-9]
\D # Any non-digit
\w # Any word character [a-zA-Z0-9_]
\W # Any non-word character
\s # Any whitespace character
\S # Any non-whitespace character
. # Any character except newline
Quantifiers
* # 0 or more times
+ # 1 or more times
? # 0 or 1 time
{n} # Exactly n times
{n,} # n or more times
{n,m} # Between n and m times
Anchors
^ # Start of string
$ # End of string
\b # Word boundary
\B # Not a word boundary
Common Regex Patterns
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Breakdown:
^[a-zA-Z0-9._%+-]+- Username (letters, numbers, dots, etc.)@- Literal @ symbol[a-zA-Z0-9.-]+- Domain name\.[a-zA-Z]{2,}$- TLD (at least 2 letters)
Phone Number (US Format)
^(\+1)?[-.\s]?\(?[0-9]{3}\)?[-.\s]?[0-9]{3}[-.\s]?[0-9]{4}$
Matches:
- 123-456-7890
- (123) 456-7890
- +1 123 456 7890
- 1234567890
URL Validation
^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)$
Password Strength
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requirements:
- At least 8 characters
- One uppercase letter
- One lowercase letter
- One digit
- One special character
Credit Card Numbers
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$
Matches:
- Visa (starts with 4)
- MasterCard (starts with 51-55)
- American Express (starts with 34 or 37)
Date Formats
YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
MM/DD/YYYY
^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}$
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]?)$
Advanced Regex Concepts
Groups and Capturing
(pattern) # Capturing group
(?:pattern) # Non-capturing group
(?<name>pattern) # Named capturing group
Lookahead and Lookbehind
(?=pattern) # Positive lookahead
(?!pattern) # Negative lookahead
(?<=pattern) # Positive lookbehind
(?<!pattern) # Negative lookbehind
Alternation
cat|dog|bird # Matches cat OR dog OR bird
Regex in Programming Languages
JavaScript
const regex = /^[a-z]+$/i;
const isMatch = regex.test("hello");
// With flags
const pattern = new RegExp("hello", "gi");
const matches = text.match(pattern);
Python
import re
pattern = r'^[a-z]+$'
match = re.match(pattern, "hello", re.IGNORECASE)
# Find all matches
matches = re.findall(r'\d+', text)
PHP
$pattern = '/^[a-z]+$/i';
$isMatch = preg_match($pattern, "hello");
// Replace
$result = preg_replace('/\s+/', '-', $text);
Best Practices
1. Keep It Simple
- Start with simple patterns
- Build complexity gradually
- Comment complex regex
2. Test Thoroughly
- Test edge cases
- Use various input samples
- Check performance with large texts
3. Use Raw Strings
- Avoid double escaping
- Makes patterns more readable
- Reduces errors
4. Optimize Performance
- Avoid excessive backtracking
- Use atomic groups when possible
- Be specific with quantifiers
5. Document Your Regex
// Matches email addresses in format: user@domain.com
// Allows alphanumeric, dots, hyphens in domain
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
Common Mistakes
1. Forgetting to Escape Special Characters
# Wrong
example.com
# Correct
example\.com
2. Greedy vs. Lazy Matching
.* # Greedy (matches as much as possible)
.*? # Lazy (matches as little as possible)
3. Not Using Anchors
# Without anchors - matches anywhere
\d{3}
# With anchors - matches only if entire string is 3 digits
^\d{3}$
4. Catastrophic Backtracking
Avoid patterns like: (a+)+b
Regex Flags
i- Case insensitiveg- Global (find all matches)m- Multiline (^ and $ match line breaks)s- Dot matches newlineu- Unicode supporty- Sticky (matches from lastIndex)
Practice Exercises
Try these patterns on our regex tester:
- Username: Letters, numbers, underscores, 3-16 characters
- Hex Color: #RGB or #RRGGBB format
- ZIP Code: US format (12345 or 12345-6789)
- Time: 24-hour format (HH:MM)
- Currency: Dollar amounts ($1,234.56)
Test and perfect your regex patterns with our free online regex tester!
Keywords
Related Posts
JSON Formatter & Validator: Complete Guide for Developers
Master JSON formatting and validation. Learn how to debug, beautify, and validate JSON data effectively.
Base64 Encoder & Decoder: What It Is and How to Use It
Understand Base64 encoding and decoding. Learn when and why to use Base64 encoding for data transmission.