Home/Regex Tools/Regex generator

Deterministic regex generator

Regex Generator

Build patterns from highlighted samples using tokenization, positional analysis, and LCS anchors.

AST first 3 samples 0 anchors
Highlight source textSelect the raw line fragments you want to clean into samples
Desired matches (sample inputs)List the strings the regex should match, one per line

Parsed samples: 3. Empty lines are ignored.

Generate mode variants and copy the one that fits your risk level.

Strict match

Validated
INV-(?<digits>\d{4})-(?<digits_2>\d{5})$

Balanced match

Validated
INV-(?<digits>\d{4})-(?<digits_2>\d{5})$

Loose match

Validated
INV-(?<digits>\d+)-(?<digits_2>\d+)$

Language exports

Active mode: balanced

JavaScript

/INV-(?<digits>\d{4})-(?<digits_2>\d{5})$/

Python

re.compile(r"INV-(?P<digits>\\d{4})-(?P<digits_2>\\d{5})$")

PHP

'/INV-(?P<digits>\d{4})-(?P<digits_2>\d{5})$/'

Golang

regexp.MustCompile(`INV-(?P<digits>\d{4})-(?P<digits_2>\d{5})$`)

AST preview

Deterministic intermediate tree
{
  "type": "sequence",
  "children": [
    {
      "type": "literal",
      "value": "INV-"
    },
    {
      "type": "namedGroup",
      "name": "digits",
      "child": {
        "type": "class",
        "classSource": "\\d",
        "min": 4,
        "max": 4
      }
    },
    {
      "type": "literal",
      "value": "-"
    },
    {
      "type": "namedGroup",
      "name": "digits_2",
      "child": {
        "type": "class",
        "classSource": "\\d",
        "min": 5,
        "max": 5
      }
    }
  ]
}
Test textMatch visualization input
Highlighted matches3 ranges
INV-2026-00123 Bad token: INV-26-1 INV-2025-77109 INV-2024-00991
[
  {
    "n": 1,
    "start": 0,
    "end": 14,
    "text": "INV-2026-00123"
  },
  {
    "n": 2,
    "start": 35,
    "end": 49,
    "text": "INV-2025-77109"
  },
  {
    "n": 3,
    "start": 50,
    "end": 64,
    "text": "INV-2024-00991"
  }
]

Tokenization snapshot

uppercaseINV0-3
punctuation-3-4
digits20264-8
punctuation-8-9
digits001239-14

Anchor tokens from LCS

Positional mode used. Token count was aligned across samples.

Fixed tokens stay literal while variable slots are inferred from token frequencies and lengths.

How it works

The generator normalizes sample text, tokenizes each sample into typed segments, then infers fixed and variable slots with positional comparison and LCS anchors before building a regex AST and compiling strict, balanced, and loose patterns.

  • Literal fragments are escaped safely and preserved in output.
  • Generation avoids nested greedy quantifiers and validates each variant against samples.

Quick examples

Invoice IDs
INV-2026-00123, INV-2025-77109
^(?<letters>[A-Z]{3})-(?<digits>\d{4})-(?<digits_2>\d{5})$
Basic emails
^(?<user>[A-Za-z0-9._-]+)@(?<domain>[\w.-]+)$

Use balanced mode first, then tighten strict mode if the pattern is for validation.

Mini FAQ

Can I generate from one sample only?

Yes. You still get deterministic token inference, but multiple samples improve generalization quality.

Why do strict and loose patterns differ?

Strict keeps exact observed bounds, while loose widens variable quantifiers for more permissive matching.

Are results engine-safe?

Patterns use bounded classes without nested greediness, then each variant is validated against your samples.