DevSwitch.net

Resetting tool...

Back to Tools
.*

Regex Generator

Build a regex from examples, or expand an existing pattern to match new text

Web Dev Tools

Note: The output always matches the examples you give. Because many regexes can match the same examples, it is a sensible generalization, not a guess at your exact intent - always review it in the RegEx Tester.

Example strings

Strategy:
Flags:

Generated regex

Generate a regex from example strings

This regex generator builds a regular expression from examples instead of a description. Type the strings you want to match, one per line, and the pattern updates as you type, with a check under it that shows each example matching in full. Anchor it with ^ and $, add the g, i or m flag, and copy the result.

It works like a regex builder that starts from data. The pattern comes from the characters you type, not from plain English, and there is no AI model involved: the same examples always give the same regex. Many patterns can match the same examples, so treat the result as a solid starting point and try it on real text before you rely on it.

Test it against real text

Paste the pattern into the Regex Tester to see every match highlighted in a longer text, with capture groups and flags.

Generalize or Literal set: same examples, two regexes

Real output of the tool for four sets of examples:

ExamplesGeneralizeLiteral set
555-1234 867-5309 999-0000 \d{3}-\d{4} 999\-0000|867\-5309|555\-1234
2024-01-15 1999-12-31 \d{4}-\d{2}-\d{2} 1999\-12\-31|2024\-01\-15
INV-2024-0012 INV-2025-1377 INV-2023-0450 [A-Z]{3}-\d{4}-\d{4} INV\-202(?:3\-0450|5\-1377|4\-0012)
active inactive pending [a-z]{6,8} (?:in)?active|pending

Generalize swaps each run of digits or letters for a class with a length, so it also matches strings you never typed: [A-Z]{3} accepts any three capitals, not only INV, and [a-z]{6,8} accepts any lowercase word of that length. Literal set matches exactly the strings you gave, packed into one pattern by the regexgen library.

For a fixed prefix with varying numbers, generate with Generalize and type the prefix back in by hand, so [A-Z]{3}-\d{4}-\d{4} becomes INV-\d{4}-\d{4}.

How Generalize reads your examples

  • Runs of one kind

    Each example is split into runs of digits, lowercase letters, capitals, spaces and other characters. 2024-01-15 becomes four digits, a dash, two digits, a dash and two digits.

  • Same shape, one pattern

    Examples with the same run order merge position by position, taking the shortest and longest length of each run as {min,max}. Symbols stay literal, and a position where they differ becomes a choice such as [/.].

  • Different shapes, alternatives

    Examples that don't line up are kept apart and joined with |. [email protected] and [email protected] differ by one dot, so they become two branches of a (?:…|…) group.

Widen an existing regex

Switch to Expand existing regex, paste your pattern and the new lines it has to match. You get two answers:

Safe union

  • Your regex and a pattern for the new text, joined as (?:old|new)
  • Everything the old regex matched still matches
  • \d{3}-\d{4} plus 555-123-4567 gives (?:\d{3}-\d{4}|\d{3}-\d{3}-\d{4})

Generalized merge

  • One cleaner pattern, built from the new text
  • Kept unchanged when your regex already matches every new line in full
  • The same input gives \d{3}-\d{3}-\d{4}, which no longer matches 555-1234, so check your old cases

Regex generator questions

No. This regular expression generator only learns from examples, which keeps the result predictable: the same lines always give the same pattern. Paste strings it should match instead of describing them.

Two to five that show the real variety: the shortest and the longest value, and each format you accept. With a single example every run keeps that exact length, so INV-2024-0012 alone allows no other lengths. Each example that differs widens the pattern a little.

Generalize only uses \d, \w, \s, character classes, (?:…) groups and {m,n} counts, which work the same in JavaScript, Python, PHP, Java, .NET and Go. Literal set escapes hyphens as \-, which JavaScript rejects with the u or v flag, so remove those backslashes if you use either. In Python and .NET, \d also matches digits from other scripts; write [0-9] if that matters.

Generalize describes the shape of your examples, not their exact values, so any string of that shape matches. Switch to Literal set for exact values, add examples that show the real variety, or replace a class with the fixed text by hand.

Anchor it when the regex checks a whole value, such as a form field or a JSON Schema pattern, which isn't anchored for you, so extra characters before or after make it fail. Leave it off to find matches inside longer text. The check under the result always tests your examples as whole strings.

Not here. The Regex Tester has quick patterns for email addresses, URLs, hex colors, numbers, words and letters. For phone numbers, paste a few in the formats you accept and let the generator build the pattern.

No. It never writes lookaheads, lookbehinds or backreferences, so rules such as "must contain a digit" have to be written by hand. The lookahead and lookbehind guide shows how.

No. The pattern is built by the page's own script in your browser, and the Literal set library loads with the page, so your examples never leave your device.