Developer

Regex Generator from Samples

Generate a regular expression from positive and negative example strings — rule-based inference with live test panel.

Loading interactive tool…

Embed this tool on your site

About this tool

Writing regular expressions from scratch is error-prone. This tool takes a different approach: list example strings that should match and, optionally, strings that should not, and it infers a pattern for you. Fixed rules recognize emails, IPv4 addresses, ISO dates (YYYY-MM-DD), http/https URLs and #hex colors. Anything else is built from the common prefix and suffix of your samples plus a middle part typed as digits, letters, alphanumerics, word characters with hyphens, or any characters, with the observed length range. A short label list explains the pattern, for example “Fixed prefix "INV-" → Digits (3-4 chars)”. Negative examples do not change the pattern; they produce a warning when the pattern still matches them, and positives it misses are flagged too. A live test panel marks each line ✓ or ✗ as you type. Options control anchoring (^ and $) and the i flag. Everything runs in your browser.

Why use it

  • Removes the need to remember regex syntax for common patterns like emails, ISO dates, IPv4 addresses, URLs and hex colors.

  • A short explanation of the pattern parts helps you understand and modify the output.

  • Live test panel gives immediate feedback without switching to a separate regex tester.

  • Negative examples add validation — you see warnings when the pattern still matches things it should not.

  • Pure JavaScript, no server — nothing you type leaves your browser.

How to use

  1. Paste example strings that the pattern SHOULD match in the left textarea, one per line — at least 3-5 examples for reliable inference.
  2. Optionally paste strings that should NOT match in the right textarea; the tool warns if the pattern still matches any of them.
  3. Check or uncheck the Anchored (^ and $) and Case-insensitive options as needed.
  4. Click Generate Regex — the tool shows the inferred pattern with a short explanation.
  5. Paste additional strings in the Live Test area to instantly see which match (✓, green) and which do not (✗, red).
  6. Click Copy to copy the pattern in /pattern/flags form.

When it helps

  • Creating input validation patterns for forms (email, phone, postal code, product code, license key) without manually writing the regex.
  • Building search filters for log files based on real example log lines.
  • Generating a starting regex for an unfamiliar string format before refining it manually.
  • Teaching regular expression concepts by showing how examples map to pattern components.
  • Quick prototyping of data parsing patterns before writing production code.

Examples

Inferring an email validator from samples

Input
Positive examples:
alice@example.com
bob+filter@subdomain.example.co.uk
chris.morgan@deep.subdomain.io

Negative examples:
foo@bar
no-at-sign.example.com
Expected result
Pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
Explanation: Anchored (^ start, $ end) → Email pattern: local@domain.tld

Inferring an ISO date pattern

Input
Positive examples:
2026-05-08
2026-12-31
2025-01-01
Expected result
Pattern: /^\d{4}-\d{2}-\d{2}$/
Explanation: Anchored (^ start, $ end) → ISO date (YYYY-MM-DD)

Inferring a hex color pattern

Input
Positive examples:
#3b82f6
#FFFFFF
#000
#a1b

Negative examples:
3b82f6
#GGHHII
Expected result
Pattern: /^#[0-9A-Fa-f]{3,6}$/
Explanation: Anchored (^ start, $ end) → Hex color code

Prefixed ID numbers

Input
Positive examples:
INV-1001
INV-2045
INV-387

Negative examples:
INV-ABC
Expected result
Pattern: /^INV-\d{3,4}$/
Explanation: Anchored (^ start, $ end) → Fixed prefix "INV-" → Digits (3-4 chars)

UUIDs fall back to a generic pattern

Input
Positive examples:
550e8400-e29b-41d4-a716-446655440000
f47ac10b-58cc-4372-a567-0e02b2c3d479
123e4567-e89b-12d3-a456-426614174000
Expected result
Pattern: /^[\w-]{36}$/
Explanation: Anchored (^ start, $ end) → Word chars + hyphen (36-36)

Tips

  • Provide at least 3-5 positive examples that represent the full range of valid inputs — coverage matters more than count, so include edge cases.
  • Use the negative examples textarea to catch false positives — paste strings that look similar but should be rejected (e.g. malformed emails like 'foo@bar' without a TLD).
  • The 'Anchored' option is on by default and is usually correct for form validation; turn it off for search-within-text use cases like grep over log files.
  • If the generated pattern looks too greedy, tighten it by hand — for example replace a generic [\w-]{36} with an exact UUID pattern.
  • Refine quoted special characters: if your data contains literal dots or parentheses, the tool escapes them to `\.` or `\(` automatically.
  • Copy the final pattern and verify in a target-language tester (regex101 for PCRE/Python/JS, regexr.com, or your editor's find/replace) before deploying.

Frequently Asked Questions

How accurate is the generated regex?
For emails, ISO dates, IPv4 addresses, URLs and hex colors it uses fixed, deliberately loose patterns (for example [^\s@]+@[^\s@]+\.[^\s@]+ for email). For other strings it builds a pattern from shared prefixes, suffixes, a character class and a length range, which often needs manual refinement. Always test with the live panel before using it in production.
Why does the pattern say it misses some positive examples?
If the inferred pattern does not match all your positive examples it will show a warning listing the mismatches. This can happen when examples are structurally inconsistent (e.g. a mix of date formats). Review the warning and either clean up the examples or adjust the generated pattern manually.
What does anchored (^ and $) mean?
Anchored patterns only match when the entire string conforms to the pattern — there is nothing before ^ or after $. Without anchors the pattern can match anywhere inside a longer string. Use anchored patterns for strict validation (input fields) and unanchored for searching within text.
Can I use the generated regex in any programming language?
The generated patterns use standard regex syntax supported by JavaScript, Python, Java, Go, and most other languages. Character class shortcuts like \d and \w are universally supported. Paste the pattern into the regex tester for your target language to confirm.
Do negative examples change the pattern?
No. The pattern is inferred from the positive examples only; negatives are tested afterwards and any that still match are listed in a warning so you can tighten the pattern by hand.
Does the tool use machine learning to generate patterns?
No. The inference is purely rule-based — it analyzes character composition, common prefixes/suffixes, and length ranges. This makes it deterministic, explainable, and works offline without any model dependencies.

Explore the category

Glossary

Anchor
The `^` and `$` metacharacters that constrain a regex to match from the start and end of a string rather than anywhere within it. `^` matches start, `$` matches end.
Character class
A set of characters matched by a single token, like `\d` for digits, `\w` for word characters, `\s` for whitespace, or `[A-Za-z]` for letters.
Quantifier
A regex token that specifies how many times the preceding element must occur, such as `{3,6}` for 3 to 6 times, `+` for one or more, `*` for zero or more, or `?` for optional.
Greedy vs lazy
Greedy quantifiers (default) match as much text as possible; lazy quantifiers (suffix `?`, e.g. `+?`) match as little as possible. Greedy `.*` between quotes consumes everything; lazy `.*?` stops at the next quote.
Lookahead
An assertion that checks what follows the current position without consuming it. Positive lookahead `(?=foo)` succeeds if `foo` is next; negative lookahead `(?!foo)` succeeds if `foo` is NOT next.
Lookbehind
An assertion that checks what precedes the current position. Positive lookbehind `(?<=foo)` and negative lookbehind `(?<!foo)`. Some older regex engines do not support lookbehind.
Capture group
Parentheses `(...)` that mark a sub-pattern for extraction. The matched text is available as group 1, 2, ... in your language's regex API. Use `(?:...)` for non-capturing grouping.
Alternation
The `|` operator chooses between alternatives. `cat|dog` matches either word. Alternation is low precedence, so `^cat|dog$` means '^cat' or 'dog$', not '^(cat|dog)$'.
A LITTLE HELP. A LOT DONE.
Good tools.
Great flow.

Your shortcut to the everyday. Create, convert, calculate,
and get back to what you love.

✓ Free to use✓ No sign-up✓ 500+ possibilities