Regex Generator
Build a regex from examples, or expand an existing pattern to match new text
Example strings
Generated regex
Additional text to also match
Safe union
Guaranteed to match the original cases and the new text.
Generalized merge
One cleaner pattern, widened where the new text differs. Best-effort - review it.
Explore More Tools
No tools match your search.
What the regex generator does
This tool turns example strings into a working regular expression, or widens a pattern you already have so it also matches new text. It reads the lines you enter, looks at the characters and their positions, then produces a pattern in JavaScript
RegExp
syntax. Everything runs in your browser.
There are two working modes. One builds a pattern from scratch based on your examples. The other takes an existing regex and stretches it to cover cases it currently misses. Both show a live checklist so you can confirm each example matches before you copy.
How the regex pattern generator works
When you paste examples, the engine reads each line and applies the strategy you choose. It never guesses your intent. It produces a pattern that provably matches every line you supplied, then leaves the review to you.
-
Generalize
: infers a semantic shape using character classes and quantifiers. For example,
555-1234and867-5309become\d{3}-\d{4}. -
Literal set
: builds a pattern that matches exactly the given strings, then reduces it. For example,
foobarandfoobazbecomefooba[rz].
You can also anchor the full match with
^
and
$
, and set the
g
,
i
, and
m
flags. The pattern updates as you type, and each example gets a check mark when it matches.
Generate a pattern from examples
The "Generate from examples" tab reads one string per line and feeds those lines to your chosen strategy. Semantic generalization looks for repeated digit, letter, or symbol runs; the literal approach keeps the exact text and only collapses shared prefixes and character choices.
- Open the tool and stay on the "Generate from examples" tab.
- Enter one example string per line.
- Pick a strategy: "Generalize" for a semantic shape or "Literal set" for exact matching.
- Optionally turn on "Anchor full match (^ … $)" and choose flags.
- Watch the checklist confirm every example, then click Copy.
Expand an existing regular expression generator flow
The "Expand existing regex" tab keeps your original pattern intact and adds coverage for new text. It parses your regex, reads the new lines, and returns two candidates so you can weigh reliability against readability.
-
Safe union
: builds
(?:originalRegex|patternForNewText). This is guaranteed to match the original cases and the new lines. - Generalized merge : a single widened pattern that reads cleaner. It is a best-effort result meant for review.
Each result carries its own match checklist, so you can see at a glance which lines pass before choosing one.
When to use it
Inferring a pattern from a finite set of strings is ambiguous by nature. Many patterns match the same examples, so the output is a sensible generalization, not a reading of your intent. Reach for this tool in these cases:
- You have sample data (IDs, phone numbers, SKUs) and want a starting pattern to refine.
- You need a regex that matches a fixed list of words without hand-writing alternations.
- An existing pattern misses a few new inputs and you want to broaden it safely.
Once you have a candidate, verify it against real input in the RegEx Tester . That page checks an existing pattern; this one produces one. If you are cleaning or reshaping data first, the Diff Checker and the wider DevSwitch tool set can help before you build the pattern.
FAQ
It is a tool that reads example strings and produces a regular expression that matches them. It analyzes the characters and positions in your samples, then applies either a semantic strategy (character classes and quantifiers) or an exact-match strategy. The result is a pattern in JavaScript syntax that you can copy and refine.
It reads one string per line. The "Generalize" strategy finds repeated runs, such as digits or letters, and replaces them with character classes and quantifiers, so
555-1234
becomes
\d{3}-\d{4}
. The "Literal set" strategy keeps the exact text and collapses shared prefixes and character choices, so
foobar
and
foobaz
become
fooba[rz]
.
"Generalize" infers a shape and will match strings beyond your examples that follow the same form, which is useful for varied data. "Literal set" matches only the exact strings you supplied, then reduces the pattern so it is shorter. Choose "Generalize" when you expect similar new inputs, and "Literal set" when you want a fixed whitelist of values.
Switch to the "Expand existing regex" tab, paste your pattern, and set its flags. Add one new example per line that the pattern should also match. The tool returns a Safe union that combines your original pattern with a pattern for the new text, and a Generalized merge that reads cleaner. Each comes with a match checklist so you can compare them.
A Safe union wraps your original pattern and a new pattern in a non-capturing alternation, written as
(?:originalRegex|patternForNewText)
. Because both sides are preserved, it is guaranteed to match every original case plus the new lines you added. It can look longer than a merged pattern, but it will not accidentally drop coverage.
Any finite set of strings can be matched by infinitely many patterns. The tool always matches the examples you gave, but it cannot know your intent, so it picks a reasonable generalization. A pattern that fits your samples might match unwanted input or miss edge cases you did not include. Reviewing keeps the result aligned with your actual data.
This tool produces a pattern from examples or expands one you have. The RegEx Tester does the opposite: you give it a finished pattern and some text, and it reports which parts match and captures. Use this page to create a pattern, then move to the tester to check it against larger, real input.
Output uses JavaScript
RegExp
syntax and is validated live in your browser. You can set the
g
,
i
, and
m
flags. Many constructs carry over to other languages, but some escape rules and flag names differ, so check the target engine before pasting a pattern into another environment.
Enabling it wraps the pattern in
^
and
$
, so the regex must match the whole string rather than a substring. This is useful when validating a field, such as a full phone number or code, where partial matches would be wrong. Leave it off when you want to find the pattern inside longer text.
No. All analysis happens in your browser with client-side JavaScript. Your example strings and patterns are not uploaded or stored on a server. This means you can work with internal identifiers or sample records without them leaving your machine, and the tool keeps running even if your connection drops.
Give enough variety to represent the range you expect. With one example the tool has little to generalize from, so the pattern may be too narrow or too broad. Several examples that differ in the parts that vary (digits, casing, length) help the engine place character classes and quantifiers where they belong. Add a counterexample check by testing afterward.
Yes, with review. Turn on "Anchor full match" so the pattern covers the whole value, then confirm it accepts every valid sample and rejects known bad ones. Because a generated pattern reflects only the examples you provided, treat it as a draft for validation rules rather than a finished spec, and tighten it where your data allows unexpected input.
