Regex from Examples

Paste examples of strings that should match (one per line); the tool infers a regex that matches all of them. Useful when you know the shape of what you want to extract but don't want to write the regex by hand. Common patterns (emails, dates, hex IDs, phone numbers) are detected and handled.

How to use the Regex from Examples

Paste the strings you want to match, one per line. Optionally paste counter-examples to refine. The tool tries three strategies: character-class-per-position (good for fixed-shape inputs like IDs), pattern detection (recognizes dates, emails, URLs, hex colors), or literal alternation (when the inputs are unrelated). The generated regex is validated against your examples.

How pattern inference works here

Give the tool a column of strings that should match and it generalises them into one regex, using one of three strategies. Character class per position compares the examples column by column and builds a class for each spot — ideal for fixed-shape inputs like order IDs. Pattern detection recognises common shapes (dates, emails, hex colours, phone numbers) and emits the idiomatic pattern for them. Literal alternation simply ORs the inputs together when they share no structure.

Optional negative examples — strings that must not match — tighten the result by ruling out an over-broad pattern. Inference is a heuristic, not magic: the generated regex is checked against your examples, but you should still read it and tighten anything too permissive before relying on it.

Common use cases

  • Extracting known-shape values — build a matcher for IDs, SKUs, or reference codes from a handful of samples.
  • Scraping log lines — turn a few example lines into a pattern that pulls the same field from all of them.
  • Drafting a validator — get a first-pass pattern for a format you can describe by example but not from memory.
  • Recognising common formats — let pattern detection hand you a tested date or email regex.
  • Learning by example — see how sample strings translate into regex constructs.

Frequently asked questions

How many examples should I provide?

Enough to show the real variation. Three near-identical strings produce a narrow pattern; a dozen that include the edge cases (shortest, longest, optional parts) produce one that generalises properly.

What are negative examples for?

They stop the tool from generating a pattern that is too loose. If your positives are all four-digit years, adding a five-digit counter-example forces the pattern to reject it.

Will it give me the shortest possible regex?

No. It aims for a correct, readable pattern that matches your examples, not a minimised one. Treat the output as a strong first draft and tighten it by hand.

How is this different from the "build regex from strings" tool?

This tool adds format detection and negative examples, so it is the better choice when your inputs are a recognised shape (dates, emails) or you need to exclude look-alikes. For raw positional alignment with a tightness slider, use the from-strings tool.
Embed this tool on your site

Free to embed, no attribution required (but appreciated). Paste this where you want the tool to appear: