When to use it
Use this tool to build and test a regular expression before you use it in code, a spreadsheet, a text editor or a search-and-replace. Seeing the matches highlighted live makes it much easier to get a pattern right, whether you are pulling email addresses from text, validating input, cleaning data or renaming files.
How to use it
- Type your pattern in Regular expression, between the slashes. Do not add the slashes yourself.
- Set the Flags by typing them or tapping g, i, m, s and u.
- Paste sample text into Test text.
- See Matches highlighted in the text, with the number of matches.
- Check Match details, which lists each match, its position and the value of every group.
- Optionally, type in Replace with to preview a search-and-replace.
Worked example
The default pattern is (\w+)@(\w+)\.com with the flags gi. In the test text it finds 2 matches: anna@example.com and SUPPORT@toolwise.com. The match details show group $1 as anna and $2 as example for the first one.
hello@ world is not matched because of the space, and bob@test.org is not matched because the pattern asks for .com.
Type $1 at $2 dot com in Replace with. The preview reads: Contact anna at example dot com or SUPPORT at toolwise dot com. This kind of replacement is handy for hiding email addresses from spam bots.
Handy building blocks
\dmatches a digit,\wa letter, digit or underscore, and\sa space or line break.+means one or more,*zero or more, and{2,4}between two and four.[abc]matches any one of a, b or c, and[^abc]anything else.
Tips
- Start small. Match one simple piece first, then add to the pattern step by step.
- Escape special characters. A dot means any character, so use
\.for a real dot. The same goes for ( ) [ ] { } + * ? ^ $ | and . - Use realistic test text. Include examples that should not match, so you can check the pattern ignores them.
- Anchor validation patterns. For checking a whole value, wrap the pattern in ^ and $.
Common mistakes
- Forgetting the g flag. Without it, only the first match is found and replaced.
- Being too greedy.
.*grabs as much as possible. Use.*?to match as little as possible. - Trusting a regex to validate emails perfectly. Real email rules are complex. Use a simple pattern and confirm by sending an email.