CoderTools

Regex Tester

Test and debug regular expressions with instant match results

Match Results

No matches found

                    

Match Details

Enter a regex pattern and test text to see detailed match information
/ /

Quick Templates

Replace Function

Use $1, $2, etc. for captured groups

                        
sed 's/pattern/replacement/g' input.txt
This sed command can be used in Unix/Linux/macOS terminal or Git Bash on Windows

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

  1. Choose a quick template or enter your regular expression pattern
  2. Set appropriate flags (g for global, i for ignore case, m for multiline)
  3. Input or load sample test text
  4. View real-time match results and detailed information
  5. Use the replace function to test substitutions

Regular Expression Flags

g
Global
Find all matches, not just the first one
i
Ignore Case
Case-insensitive matching
m
Multiline
^ and $ match line boundaries

Common Regular Expression Patterns

Basic Patterns
\\d - Any digit (0-9)
\\w - Word character (a-z, A-Z, 0-9, _)
\\s - Whitespace character
. - Any character except newline
Quantifiers
+ - One or more
* - Zero or more
? - Zero or one
{n} - Exactly n times
Anchors
^ - Start of line
$ - End of line
\\b - Word boundary

Practical 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.

Matches (555) 123-4567, 555.123.4567, 555-123-4567

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.

Matches https://example.com, http://www.site.org/path

Strong Password Validation

^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d@$!%*?&]{8,}$

Requires at least 8 characters with lowercase, uppercase, and digit.

Matches Password123, MyStr0ngP@ss

Advanced Features

Capturing Groups
Use parentheses () to capture parts of matches for replacement
([0-9]{4})-([0-9]{2})-([0-9]{2})
Lookahead/Lookbehind
Match based on what comes before/after without including it
(?=.*password)
Non-capturing Groups
Group without capturing using (?:pattern)
(?: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
Example: Pattern: error
Result: Prints all lines containing 'error'

Replace Mode (With Replacement)

When replacement text is provided, generates a substitution command.

sed 's/pattern/replacement/flags' input.txt
Example: Pattern: foo, Replace: bar
Result: Replaces all 'foo' with 'bar'
Note: Special characters are automatically escaped. The generated command works in Unix/Linux/macOS terminals and Git Bash on Windows.

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.