gizmobench

Regex Generator

Paste the strings that should match, one per line, put a minus sign and a space in front of the ones that must not, and the pattern appears beside them. It is built by a rule you can follow: the first positive example is cut into runs of digits, letters, spaces and repeated punctuation, a run that reads the same in every positive example is kept as literal text and escaped so a dot stays a dot, and a run that differs is generalised to one of five counted tokens and bounded by the shortest and longest length your examples actually showed. Three invoice numbers give ^INV-\d{4}-\d{4}$. Every example you gave, positive and negative, is then run against that pattern by your browser's own engine and scored on screen, and a negative that still matches is printed as a conflict rather than quietly designed around. Each run has its own menu, so you can widen or narrow any part of the pattern, and the pattern itself is an editable field. Up to 200 examples of up to 500 characters each, nothing uploaded and no account.

selectednothing yet
patternno flags

The pattern, what each piece of it means, and every example scored against it, appear here.

Positives
-
Negatives
-
Flags

Waiting for an example.

The runs of your first example

Each run of the first positive example gets a control here, once there is an example to cut up.

Match

A pattern built from examples is a guess at the rule behind them, not the rule itself, so it can match strings you never had in mind. Paste the ones that must not match as negative examples, with a minus sign and a space in front, and watch what the table says.

  • Three invoice numbersboth digit runs selected
    ^INV-\d{4}-\d{4}$
  • A literal dot, not selectedtext, not a wildcard
    escaped to \.
  • A negative that still matchesthe examples conflict
    named, never narrowed silently
Accuracy. The pattern is built from the spans you select and a fixed, published set of tokens, so it is deterministic rather than inferred, and every example you gave is run against it and scored on screen. Quantifiers are always bounded, so nothing written here can backtrack catastrophically. It generalises only as far as your examples reach, and the pattern is yours to edit.

Common questions

How does it decide which parts to generalise?
By comparing your positive examples. The first one is the template: it is cut into runs of digits, letters, spaces and repeated punctuation, and every other positive example with the same run shape votes on each run. A run that reads the same in all of them stays literal text. A run that differs is generalised, and counted by the shortest and the longest your examples showed, so three four-digit years and one three-digit one give \d{3,4} rather than a guess. Nothing beyond that is inferred, and every run has a menu you can override.
Why did it escape the dot in my examples?
Because a dot in an example is a dot. An unescaped dot in a regular expression matches any character, so leaving it bare would quietly widen the pattern to strings you never showed it. Literal runs are escaped for every character that is special outside a character class, which is why a hyphen comes through as a hyphen while a dot comes through escaped. If you want that position to be a wildcard, choose Any character for that run and the escape goes.
A string I did not want still matches. What do I do?
Add it as a negative example: a line with a minus sign and a space in front of it. Every negative is run against the finished pattern too, and one that still matches is named as a conflict instead of being designed around. That conflict is the honest answer, because these tokens cannot tell those two strings apart. Narrow a run yourself, add a positive example that differs from the negative, or edit the pattern by hand.
Can I edit the pattern it gives me?
Yes. The pattern is an input, not a label: type in it and your examples are re-scored against what you typed, and Rebuild puts the built pattern back. One kind of edit is refused. A repeat with no upper bound wrapped around another repeat or an alternation, like (a+)+, and two unbounded repeats side by side, like \d*\d*, are the shapes that backtrack for a very long time on text that does not match, and a run like that cannot be interrupted once it starts. The page says so instead of running it. The regex tester runs a pattern like that in a worker it can stop.
Is this the regular expression my own code will run?
It is JavaScript regular expression syntax, matched here by your own browser's engine: every score on the page comes from calling test on a RegExp built from exactly the pattern and flags shown. The flag settings offered are none and i, and the pattern is anchored to the whole string unless you switch it to match anywhere. Most of what this tool writes, \d, \w, \s, [A-Za-z] and counted repeats, means the same thing in Python, PCRE and Go.
How many examples can I paste in?
Up to 200 examples, each up to 500 characters, and 20,000 characters in the box altogether. Past any of those the page names the limit you passed and the size that arrived, rather than building a pattern out of part of what you pasted. The first example is also cut into at most 60 runs, because a pattern with more pieces than that is longer than it is useful.
Does anything I paste leave the page?
No. There is no upload, no account and no request of any kind: the pattern is built and the examples are matched in this tab, by this page's own code. What you type is kept in your browser's local storage so the page opens where you left it, and the Start over button above the heading forgets it.

The pattern is built from the spans you select and a fixed, published set of tokens, so it is deterministic rather than inferred, and every example you gave is run against it and scored on screen. Quantifiers are always bounded, so nothing written here can backtrack catastrophically. It generalises only as far as your examples reach, and the pattern is yours to edit.