Build Regex from Example Strings

You know which strings should match but you don't want to write the regex by hand. Paste your examples — phone numbers, product codes, file paths — and this tool infers the pattern: literal where all examples agree, character classes where they differ. A generic-to-specific slider controls how loose the pattern is.

balanced

How to use the Build Regex from Example Strings

Paste example strings. The tool finds positions where all examples agree (becomes literal) and positions where they differ (becomes a character class). The slider adjusts how tight the pattern is:

  • Loose — generic classes (\w, \d, .)
  • Balanced — character ranges where appropriate
  • Strict — specific character sets seen in examples
  • Literal — alternation of all examples

From sample strings to a pattern, with a tightness dial

This builder lines your example strings up by position and looks at each column. Where every example agrees on a character, the output keeps it as a literal; where they differ, it emits a character class covering what it saw. The result is a pattern shaped exactly like your data — perfect for fixed-format strings such as SKU-2026-001.

A specificity slider controls how tight that class is. Loose uses broad shorthands (\w, \d, .); balanced uses sensible ranges; strict restricts each position to the exact characters observed; and literal falls back to alternating the whole inputs. You dial in how much new input you want the pattern to accept.

Common use cases

  • Product and SKU codes — derive a validator from a list of real codes in seconds.
  • Fixed-format identifiers — match invoice numbers, ticket refs, or part numbers that follow a template.
  • File-naming conventions — build a pattern that accepts your naming scheme and rejects stray files.
  • Generating a regex from sample data — when you have the data but cannot write the pattern by hand.
  • Tuning strictness — slide between a forgiving and a locked-down pattern to suit the job.

Frequently asked questions

How is this different from the "regex from examples" tool?

This one is built around positional alignment and a strictness slider, which makes it ideal when every example has the same shape and length. The from-examples tool adds format detection and negative examples for less regular input. They are complementary.

What if my examples have different lengths?

Positional alignment works best on same-length strings. With mixed lengths the columns stop lining up and the pattern loosens, so group your inputs by length and run each group separately for the cleanest result.

Does the pattern anchor to the whole string?

Only if you want it to. The anchor toggle wraps the result in ^...$ so it must match the entire value; turn it off to allow the pattern to match inside a larger string.

Is the output ready for production?

Use it as a strong starting point. Always test it against fresh data, and for any security-sensitive validation review it by hand — an inferred pattern reflects only the samples it was shown.
Embed this tool on your site

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