Regex Tester
Test and debug regular expressions with instant match results
Match Results
Match Details
Quick Templates
Replace Function
sed 's/pattern/replacement/g' input.txt
Regular Expression Testing Tool Documentation
What is Regular Expression?
Regular expressions (regex) are powerful pattern-matching tools used in programming to search, extract, and manipulate text. They use special characters and syntax to define search patterns that can match strings of characters.
Regular expressions are widely used for data validation, text processing, log analysis, and search-and-replace operations. They provide a concise and flexible way to identify patterns in text.
How to Use This Tool
- Choose a quick template or enter your regular expression pattern
- Set appropriate flags (g for global, i for ignore case, m for multiline)
- Input or load sample test text
- View real-time match results and detailed information
- Use the replace function to test substitutions
Regular Expression Flags
g
i
m
Common Regular Expression Patterns
\\d - Any digit (0-9)\\w - Word character (a-z, A-Z, 0-9, _)\\s - Whitespace character. - Any character except newline+ - One or more* - Zero or more? - Zero or one{n} - Exactly n times^ - Start of line$ - End of line\\b - Word boundaryPractical Examples
Email Address Validation
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}
Matches: one or more valid characters, followed by @, domain name, and 2+ letter extension.
Phone Number (US Format)
\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})
Captures area code, exchange, and number with flexible formatting.
URL Matching
https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)
Matches HTTP/HTTPS URLs with optional www prefix and various path formats.
Strong Password Validation
^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d@$!%*?&]{8,}$
Requires at least 8 characters with lowercase, uppercase, and digit.
Advanced Features
([0-9]{4})-([0-9]{2})-([0-9]{2})
(?=.*password)
(?:http|https)://
Tips and Best Practices
- Start simple and build complexity gradually
- Use the global flag (g) to find all matches
- Escape special characters with backslash (\)
- Test thoroughly with various input examples
- Use capturing groups for complex replacements
- Consider performance with large texts
sed Command Generation
This tool automatically generates equivalent sed commands based on your regex pattern and replacement text, making it easy to use regex in Unix/Linux command line.
Search Mode (Empty Replacement)
When replacement text is empty, generates a search command to print matching lines only.
sed -n '/pattern/p' input.txt
errorReplace Mode (With Replacement)
When replacement text is provided, generates a substitution command.
sed 's/pattern/replacement/flags' input.txt
foo, Replace: barLearning Resources
Online Resources
Best Practices
Start Simple, Then Add Complexity
Build your regex incrementally. Start with a simple pattern that matches some cases, then refine to handle edge cases. Testing each step prevents complex debugging.
Use Non-Capturing Groups When Possible
Use (?:...) instead of (...) when you don't need to capture the group. This improves performance and keeps capture group numbers cleaner.
Anchor Your Patterns
Use ^ and $ to anchor patterns to line/string boundaries when appropriate. Unanchored patterns may match unexpected substrings.
Be Specific with Character Classes
Instead of .* (which matches everything), use specific classes like [a-zA-Z]+ or \d{3}. Overly broad patterns cause incorrect matches and backtracking issues.
Escape Special Characters
Remember to escape . * + ? [ ] ( ) { } ^ $ \ | when you want to match them literally. Use this tool's Regex Escape format for help.
Test with Edge Cases
Test your regex with empty strings, very long strings, special characters, and Unicode. Real-world data often contains unexpected patterns.
Troubleshooting
Why doesn't my pattern match anything?
Check flags: 'g' for multiple matches, 'i' for case-insensitive, 'm' for multiline. Also verify special characters are properly escaped.
Why does my regex cause the browser to hang?
Catastrophic backtracking from nested quantifiers like (a+)+ or (a|aa)*. Simplify your pattern or use possessive quantifiers if supported.
Why do my captured groups return unexpected values?
Group numbering starts at 1 for the first (...). Group 0 is the entire match. Non-capturing groups (?:...) don't consume group numbers.
Why does my pattern work differently in different languages?
Regex flavors vary. JavaScript doesn't support lookbehind in older browsers. Features like \b, \w may behave differently with Unicode. Test in your target environment.