Regex Generator from Sample

Writing a regex by hand for something like an order number, an invoice reference or a date is fiddly and easy to get wrong. Paste one example string that matches the format you want, and this tool reads it left to right, turning each run of digits, uppercase letters and lowercase letters into a character class of the right length while keeping punctuation and symbols as literal characters. You get an anchored pattern to use as a starting point and refine by hand.

How it works

  1. 1

    Enter a sample

    Type or paste one example string that represents the format you want to match, such as `Order #12345 placed on 2026-04-16`.

  2. 2

    Generate the pattern

    The tool scans the sample character by character and replaces each run of the same kind of character with a character class sized to its length.

  3. 3

    Read the breakdown

    Below the pattern, a component list explains each piece, so you can see which part matched digits, letters or literal text.

  4. 4

    Copy and refine

    Use the pattern as a starting point. Adjust the fixed lengths or the classes by hand so it fits the full range of your real data.

How the pattern is built

The tool reads the sample from left to right. Whenever it meets a run of the same kind of character, it emits a character class sized to that run:

  • a run of digits becomes \d{n}, where n is how many digits there are
  • a run of uppercase A-Z becomes [A-Z]{n}
  • a run of lowercase a-z becomes [a-z]{n}
  • a single space becomes \s, and several spaces become \s{n}
  • anything else (punctuation, symbols, accented or non-Latin letters) is kept as an escaped literal character

The finished pattern is wrapped in ^ and $ so it matches the whole string from start to end, not just a fragment.

Example

For the sample Order #12345 placed on 2026-04-16 the tool produces:

/^[A-Z]{1}[a-z]{4}\s\#\d{5}\s[a-z]{6}\s[a-z]{2}\s\d{4}\-\d{2}\-\d{2}$/

Order is one uppercase letter followed by four lowercase letters; 12345 becomes \d{5}; and the date 2026-04-16 becomes \d{4}\-\d{2}\-\d{2}.

What it does and does not do

  • The lengths are fixed. 12345 becomes \d{5}, which matches exactly five digits, not “one or more”. If the count varies in your data, change {5} to + or a range such as {3,8} by hand.
  • It works from a single sample. It mirrors the structure of the one string you give it, so it cannot generalise across several examples on its own. Feed it the most representative string.
  • Only A-Z and a-z count as letters. Accented and non-Latin characters are kept as literals, so replace them with a class such as \w or \p{L} if you want them to match a wider set.
  • A regex cannot express everything. Things like balanced brackets are beyond any regex, no matter the sample. For nested structures you need a parser, not a pattern.

A good workflow

  1. Generate a pattern from one clear, representative sample.
  2. Relax the fixed counts ({5} becomes + or {3,8}) wherever the length varies.
  3. Paste the pattern into the Regex Tester with a larger set of real strings.
  4. Adjust until it matches everything it should and nothing it should not.

Frequently Asked Questions

One. The generator reads a single example string and mirrors its structure. Pick the string that best represents your format, then widen the pattern by hand for the other cases.

The tool counts the exact characters in your sample, so five digits become \d{5}. If the count varies in real data, replace {5} with +, * or a range such as {3,8}.

The anchors make the pattern match the whole string from start to end. Remove them if you want to find the pattern inside a longer piece of text.

It recognises A-Z and a-z as letters. Any other character, including accented letters, Cyrillic or CJK, is kept as a literal. Swap it for a class like \w or \p{L} by hand if you want it to match more.

Standard syntax (\d, [A-Z], {n}) that works unchanged in PCRE, JavaScript, Python and most other engines.

Related Tools

Tool available in other languages