Regex Tester
A tool for testing and debugging regular expressions. Supports various flags and provides detailed match information with captured groups.
Flags
- g - Global: find all matches
- i - Case Insensitive
- m - Multiline: ^ and $ match newlines
- s - Dotall: . matches newlines
- u - Unicode: full Unicode support
Common Syntax
.- Any character\d- Digit (0-9)\w- Word character (a-z, A-Z, 0-9, _)\s- Whitespace*- 0 or more+- 1 or more?- 0 or 1[abc]- Either a, b, or c(abc)- Capture group
Key Features
🎯 Real-Time Testing
- Instant pattern matching results
- Visual highlighting of matches
- Match count display
🚩 Flag Support
- g (Global): Find all matches
- i (Ignore Case): Case-insensitive matching
- m (Multiline): ^ and $ match line breaks
- s (Dotall): . matches newlines
- u (Unicode): Full Unicode support
📊 Detailed Results
- Match positions (start-end)
- Captured groups display
- Match values
Common Regex Patterns
Basic Patterns
. Any character (except newline)
\d Digit (0-9)
\w Word character (a-z, A-Z, 0-9, _)
\s Whitespace (space, tab, newline)
\D Non-digit
\W Non-word character
\S Non-whitespace
Quantifiers
* 0 or more
+ 1 or more
? 0 or 1
{n} Exactly n times
{n,} n or more times
{n,m} Between n and m times
Anchors
^ Start of string/line
$ End of string/line
\b Word boundary
\B Non-word boundary
Character Classes
[abc] Either a, b, or c
[^abc] Not a, b, or c
[a-z] Range (a to z)
[a-zA-Z] Range (a-z or A-Z)
[a-zA-Z0-9] Range (alphanumeric)
Groups
(abc) Capturing group
(?:abc) Non-capturing group
(?<name>) Named capturing group
(a|b) Alternation (a or b)
Usage Examples
1. Email Validation
// Pattern
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
// Test
emailPattern.test("user@example.com"); // true
emailPattern.test("invalid.email"); // false
2. Phone Number Extraction
// Pattern (US format)
const phonePattern = /\d{3}-\d{3}-\d{4}/g;
// Extract
const text = "Call 123-456-7890 or 098-765-4321";
const phones = text.match(phonePattern);
// ["123-456-7890", "098-765-4321"]
3. URL Matching
// Pattern
const urlPattern = /https?:\/\/[^\s]+/g;
// Extract
const text = "Visit https://example.com and http://test.org";
const urls = text.match(urlPattern);
// ["https://example.com", "http://test.org"]
4. Password Validation
// Pattern: 8+ chars, 1 uppercase, 1 lowercase, 1 number, 1 special char
const passwordPattern = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
// Test
passwordPattern.test("Pass123!"); // true
passwordPattern.test("password"); // false
5. Date Extraction
// Pattern (YYYY-MM-DD)
const datePattern = /\d{4}-\d{2}-\d{2}/g;
// Extract
const text = "Events: 2024-01-15 and 2024-12-31";
const dates = text.match(datePattern);
// ["2024-01-15", "2024-12-31"]
Programming Language Examples
JavaScript
// Basic matching
const pattern = /\d+/g;
const text = "I have 3 apples and 5 oranges";
const matches = text.match(pattern);
console.log(matches); // ["3", "5"]
// Using test()
const emailPattern = /^[\w.-]+@[\w.-]+\.\w+$/;
console.log(emailPattern.test("user@example.com")); // true
// Using exec() with groups
const datePattern = /(\d{4})-(\d{2})-(\d{2})/;
const match = datePattern.exec("2024-01-15");
console.log(match[1]); // "2024"
console.log(match[2]); // "01"
console.log(match[3]); // "15"
// Using replace() with regex
const result = "Hello World".replace(/hello/i, "Hi");
console.log(result); // "Hi World"
Python
import re
# Basic matching
pattern = r'\d+'
text = "I have 3 apples and 5 oranges"
matches = re.findall(pattern, text)
print(matches) # ['3', '5']
# Using search()
email_pattern = r'^[\w.-]+@[\w.-]+\.\w+$'
if re.search(email_pattern, "user@example.com"):
print("Valid email")
# Using groups
date_pattern = r'(\d{4})-(\d{2})-(\d{2})'
match = re.search(date_pattern, "2024-01-15")
print(match.group(1)) # "2024"
print(match.group(2)) # "01"
print(match.group(3)) # "15"
# Using sub() for replacement
result = re.sub(r'hello', 'Hi', "Hello World", flags=re.IGNORECASE)
print(result) # "Hi World"
Java
import java.util.regex.*;
// Basic matching
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("I have 3 apples and 5 oranges");
while (matcher.find()) {
System.out.println(matcher.group()); // "3", "5"
}
// Using matches()
Pattern emailPattern = Pattern.compile("^[\\w.-]+@[\\w.-]+\\.\\w+$");
boolean isValid = emailPattern.matcher("user@example.com").matches();
System.out.println(isValid); // true
// Using groups
Pattern datePattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher dateMatcher = datePattern.matcher("2024-01-15");
if (dateMatcher.find()) {
System.out.println(dateMatcher.group(1)); // "2024"
System.out.println(dateMatcher.group(2)); // "01"
System.out.println(dateMatcher.group(3)); // "15"
}
// Using replaceAll()
String result = "Hello World".replaceAll("(?i)hello", "Hi");
System.out.println(result); // "Hi World"
Best Practices
1. Start Simple
Begin with basic patterns and gradually add complexity. Test each addition to ensure it works as expected.
2. Use Character Classes
Instead of (a|b|c|d|e), use [a-e] for better readability and performance.
3. Be Specific
❌ Bad: .*@.*
✅ Good: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
4. Avoid Greedy Quantifiers
Use non-greedy quantifiers (*?, +?) when appropriate:
Greedy: <.*> matches "<div>text</div>"
Non-greedy: <.*?> matches "<div>" and "</div>" separately
5. Use Anchors
Add ^ and $ for exact string matching:
Without: \d{3}-\d{4} matches anywhere in string
With: ^\d{3}-\d{4}$ matches exact format only
6. Escape Special Characters
Remember to escape: . * + ? ^ $ { } [ ] ( ) | \
7. Test Thoroughly
Test your regex with:
- Valid inputs
- Invalid inputs
- Edge cases
- Empty strings
- Very long strings
Common Regex Use Cases
1. Data Validation
Email: ^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Phone: ^\d{3}-\d{3}-\d{4}$
ZIP Code: ^\d{5}(-\d{4})?$
URL: ^https?://[^\s]+$
IP Address: ^(\d{1,3}\.){3}\d{1,3}$
2. Data Extraction
Numbers: \d+
Words: \w+
Dates: \d{4}-\d{2}-\d{2}
Times: \d{2}:\d{2}(:\d{2})?
Hashtags: #\w+
3. Text Processing
Remove HTML tags: <[^>]+>
Remove extra spaces: \s+
Match quoted text: "[^"]*"
Match parentheses: \([^)]*\)
4. Code Analysis
Function names: function\s+(\w+)
Variable declarations: (let|const|var)\s+(\w+)
Comments: //.*|/\*[\s\S]*?\*/
Import statements: import\s+.*\s+from\s+['"].*['"]
FAQ
Q: What's the difference between greedy and non-greedy matching?
A: Greedy (*, +) matches as much as possible, non-greedy (*?, +?) matches as little as possible.
Q: How do I match a literal dot or other special character?
A: Escape it with a backslash: \. matches a literal dot.
Q: What does (?=...) mean?
A: It's a positive lookahead assertion. It matches if the pattern inside is found, but doesn't include it in the match.
Q: How can I make my regex case-insensitive?
A: Use the i flag, or in the pattern use (?i) before your pattern.
Q: Why isn't my regex matching across newlines?
A: Use the s (dotall) flag to make . match newlines, or use [\s\S] instead of .
Q: How do I test if a string matches exactly?
A: Use anchors: ^pattern$ ensures the entire string matches.
Q: What's the difference between [] and ()?
A: [] defines a character class (any one character), () defines a group (sequence of characters).
Q: How do I match Unicode characters?
A: Use the u flag and Unicode property escapes like \p{L} for any letter.
Related Tools
- Find & Replace - Find and replace text with regex
- Text Comparison - Compare two text blocks
- Base64 Encoder - Encode and decode Base64