How to test a regular expression
- Type your pattern between the slashes. The explanation below the text updates as you type.
- Paste the text to search into Test text. Every match is highlighted, in two alternating colors so neighbors can be told apart.
- Tick the flags you need. Keep g on to find all matches, and add i to ignore case.
- Open Match details to see where each match is, and the text of each capture group.
- To change the text, type a replacement and copy the result. Use $1, $2 or $<name> for groups.
What a regular expression is
A regular expression, or regex, is a short pattern that describes a set of strings. Instead of searching for one fixed word, you can search for things like “a date written as four digits, a dash, two digits, a dash and two digits”, which is the pattern \d{4}-\d{2}-\d{2}. Programmers use regular expressions to find text, check that input has the right shape, pull values out of logs and files, and rewrite text in bulk. Editors, command-line tools and every major programming language support them.
The syntax you need most
- Characters: most characters match themselves. A backslash turns a special character into a plain one, so
\.matches a real dot. - Classes:
\dis a digit,\wa letter, digit or underscore,\sa space or line break, and.almost any character. Capital letters mean the opposite:\Dis a non-digit.[abc]matches one of a, b or c;[^abc]matches anything else;[a-z]is a range. - Repeats:
*means zero or more,+one or more,?zero or one,{3}exactly three, and{2,5}two to five. By default repeats take as much as they can; add?after them (+?) to take as little as possible. - Positions:
^is the start and$the end of the text (or of a line with the m flag), and\bis the edge of a word. - Groups:
(…)captures part of the match so you can read it later or reuse it in a replacement;(?:…)groups without capturing;(?<name>…)gives a group a name.|means “or”. - Look-around:
(?=…)and(?!…)check what comes next, and(?<=…)and(?<!…)check what came before, without including it in the match. - Back-references:
\1or\k<name>match the same text again, which is how the “Repeated words” example finds “the the”.
Flags
Flags change how the whole pattern behaves. g finds all matches, i ignores case, m lets ^ and $ match at every line, and s lets the dot match line breaks. u switches on full Unicode support, which you need for emoji, for characters outside the basic range and for properties such as \p{L} (any letter in any language). v is an upgraded Unicode mode and cannot be combined with u, and y is the sticky flag that only matches at the exact position where the search starts.
Common patterns, and their limits
The example buttons load working patterns for email addresses, web addresses, dates, IPv4 addresses, hex colors and repeated words. They are good starting points, but treat them as rough filters rather than complete validators. The email pattern accepts most real addresses and rejects obvious mistakes, but the full rules for email are far more complicated, and the only sure test of an address is to send a message to it. The date pattern checks that the month is 01 to 12 and the day 01 to 31 but not that the day exists in that month. When something must be exactly right, use a purpose-built parser, such as a date library or the URL class, and keep the regex for the rough shape.
Slow patterns
Most regular expression engines, including the one in JavaScript, work by trying one way of matching and going back to try another when it fails. Usually that is fast. But a pattern with nested repeats, such as (a+)+$, can have an astronomical number of ways to split the text, and on text that nearly matches, the engine tries them all. Adding one more character can double the time. This is called catastrophic backtracking, and in a real program it can freeze a page or a server, which attackers have used to bring down websites. This tester runs the pattern in a background worker and stops it after two seconds, so you can find such a pattern safely. To fix one, avoid repeating a group that can match the same text in more than one way, make the parts more specific (for example [a-z]{1,20} in place of .*), and limit the length of the text you test.
Use your pattern in code
The Use this pattern in code box above writes the code for you, with the pattern, flags and replacement already turned into each language’s syntax and your test text (up to 300 characters) set as text, and notes where a language reads the pattern differently. For example, Python writes named groups as (?P<name>…), Go can’t run look-arounds or back-references, and in Python and .NET \d also matches digits from other scripts. By hand, the basics are:
- JavaScript:
text.match(/pattern/g)for all matches,text.matchAll(/pattern/g)for matches with groups, andtext.replace(/pattern/g, '$2 $1')to replace. - Python:
re.findall(r'pattern', text)andre.sub(r'pattern', r'\2 \1', text); therbefore the quote keeps backslashes as they are. - PHP:
preg_match_all('/pattern/', $text, $m)andpreg_replace('/pattern/', '$2 $1', $text). - Java:
Pattern.compile("pattern").matcher(text); remember to double every backslash in the string. - C#:
Regex.Matches(text, @"pattern"); the@before the quote avoids doubling backslashes. - Go:
regexp.MustCompile(`pattern`). Go’s engine guarantees fast matching, and for that reason it does not support look-around or back-references. - Command line:
grep -E 'pattern' filefor extended expressions, orgrep -Pon Linux for Perl-style ones.
Tips
- Start small and add one piece at a time, checking the highlighted result after each step.
- Test text that should not match as well as text that should. A pattern that matches too much is as wrong as one that matches too little.
- Anchor when you validate. Use
^and$to make sure the whole text fits, not just a part of it. - Prefer specific classes such as
[^,]*(anything but a comma) over a greedy.*, which is both faster and less surprising.
Related tools: compare two versions of a text with the text compare tool, format data with the JSON formatter, or change letter case with the case converter.
Frequently asked questions
Which regular expression flavor does this tester use?
It uses the JavaScript engine of your own browser, the same one that runs regular expressions in web pages and Node.js. That makes it exact for JavaScript and TypeScript. Most everyday patterns work the same in Python, PHP, Java, C# and Ruby, but some features differ, for example named groups are written (?<name>…) in JavaScript and (?P<name>…) in Python, and JavaScript has no \A, \Z, atomic groups or possessive quantifiers.
Is my text or pattern uploaded?
No. The pattern runs in your browser, in a background worker, and nothing is sent anywhere. The page remembers your last pattern, flags and text on this device so they are there when you come back, and Clear removes them.
How do I share a regex with someone?
Press Copy a link to this pattern. The link holds the pattern, flags and replacement, and the test text too if you tick that box, after a # sign. Browsers don’t send that part of a link to the web server when it is opened, but anyone you give the link to can read what it holds, so leave out text that is private.
Why was my pattern stopped after 2 seconds?
Some patterns can take an enormous amount of time on certain text. This is called catastrophic backtracking, and it happens when repeats are nested, such as (a+)+$, so the engine has to try exponentially many ways to split the text before it can say no. The tester runs your pattern in a worker and ends it after 2 seconds so that the page never freezes. In your own program such a pattern could freeze it or be used to attack it, so it is worth fixing.
What do the flags g, i, m, s, u, v and y do?
g finds every match instead of only the first. i ignores upper and lower case. m makes ^ and $ match at the start and end of each line. s lets the dot match line breaks. u turns on full Unicode handling, for example for emoji and \p{…} properties. v is a newer, stricter Unicode mode with set operations in character classes, and cannot be combined with u. y makes the match start exactly where the previous one ended.
How do I replace text with the groups I captured?
Type a replacement under the test text. In it, $1, $2 and so on stand for the first, second and later capture groups, $<name> stands for a named group, $& for the whole match and $$ for a dollar sign. For example the pattern (\w+) (\w+) with the replacement $2 $1 swaps two words. The result appears below, ready to copy.
Can I use a pattern from here in another language?
Yes. Under Use this pattern in code, pick JavaScript, Python, PHP, Java, C# or Go to get code with the pattern, flags, replacement and a short test text already converted, string escaping included, ready to paste, plus notes on what that language reads differently, such as Python’s (?P<name>…) groups or Go’s lack of look-arounds. Test the final code in your project before relying on it.