Developer and Text Tools

How to Build a Regular Expression from Examples

Write regex the easy way: list strings that should and should not match, pick the right candidate, understand each part and export it for your language.

Saizul Amin
Saizul Amin3 Nov 2026 · 3 min read
How to Build a Regular Expression from Examples

How to Build a Regular Expression from Examples

Regular expressions are one of the most useful tools in programming and one of the hardest to read. Most people who need one know exactly what their data looks like: invoice numbers like INV-2024-0042, product codes like AB-1234, version tags like v2.10.3. What they do not remember is whether a digit is written with a backslash and d, how to say exactly four times, or which characters need escaping. This guide shows a more natural way to work: start from examples and let a tool propose the pattern, using the free Regex Generator from Examples.

Positive and negative examples

A pattern is defined as much by what it rejects as by what it accepts. If you only show the generator AB-1234 and XY-0001, the answer could be anything from those two exact strings to "any text with a hyphen". Negative examples narrow it down. Good negatives are near misses: the same code in lowercase, without the hyphen, with one digit too few, or with three letters instead of two. Each near miss rules out a looser pattern.

Two or three positives are usually enough for regular codes. Add more when the values vary in length or shape.

How candidates are built

The generator reads each positive example as a sequence of runs: a run of uppercase letters, a hyphen, a run of digits. If all positives share the same sequence, it can write a pattern class by class. It then produces a ladder of candidates from strict to loose: exact classes with the lengths seen in the examples, letters merged regardless of case, letters and digits merged, and versions that accept one or more of each run. It also tries the common prefix and suffix of all examples, and as a last resort a list of the examples themselves.

Every candidate is tested against both boxes. Those that accept every positive and reject every negative rise to the top. Among correct candidates the stricter one is usually preferred, because it rejects odd input you have not anticipated, unless your examples differ in length, in which case a pattern that allows variable length is preferred.

Step by step

  1. Type the strings that should match, one per line, or start from a preset.
  2. Type the near misses that must not match.
  3. Decide whether the whole string must match. For form validation it should; for finding codes inside text it should not.
  4. Look at the candidates and press Use on the best one. Each shows how many positives it matched and how many negatives it wrongly accepted.
  5. Read the explanation. The pattern is split into parts with a plain sentence for each, which is the best way to spot a pattern that is right for the wrong reason.
  6. Paste some real text into the test box and check the highlighted matches.
  7. Pick your language and copy the code. Escaping differs between languages, especially in Java strings where every backslash must be doubled.

Knowing the limits

Learning from examples finds structure, not meaning. The generator cannot know that a month runs from 01 to 12 or that a check digit must add up. Formats with official rules, such as IBANs, postcodes or email addresses, deserve validators built on those rules. Use the generated pattern as a first filter and add proper validation where it matters.

Also be careful with patterns that will run on long, untrusted input. Nested quantifiers can take a very long time on certain strings. The simple sequential patterns this generator produces are not prone to that, but hand edits can be.

Privacy

Examples are processed in your browser and saved only in local storage on your device, so you can safely use real customer codes while testing.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages