Skip to main content

Regex generator that derives optimized patterns from example strings.

Project description

rector — Regex Generator

Rector turns example strings into optimized regular expressions you can reuse in search, validation, or routing rules. It ships with a trie-based engine that produces compact, deterministic patterns (also used by the CLI).

Installation

pip install rector

Library usage

from rector import TrieRegexGenerator

# Build a minimal trie-based pattern for larger sets or ranges
numbers = (
    TrieRegexGenerator()
    .add_examples([str(n) for n in range(10, 1000)])
    .generate()
)
assert numbers.pattern == "^[1-9][0-9]{1,2}$"

# Generate from small fixed-length samples
words = TrieRegexGenerator().add_examples(["cat", "car", "cap"]).generate()
assert words.pattern == r"^ca[prt]$"

CLI

Pipe examples to rector and it prints the generated pattern to stdout:

printf "cat\ncar\ncap\n" | rector
# -> ^ca[prt]$

The CLI anchors patterns by default and exits with status 1 if no stdin input is provided. Use --dense-threshold to adjust when near-contiguous character sets are collapsed into ranges (accepts fractions.Fraction strings like 3/4 or 0.6; default when flag is present without a value: 8/10).

Behavior notes

  • Patterns anchor to start/end unless you pass anchor_start=False or anchor_end=False.
  • Examples must be strings; TrieRegexGenerator raises TypeError otherwise.
  • TrieRegexGenerator builds a trie, minimizes it, and compacts ranges/quantifiers (using the density threshold for near-contiguous classes); it also accepts empty lines as valid examples.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rector-0.1.8.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rector-0.1.8-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file rector-0.1.8.tar.gz.

File metadata

  • Download URL: rector-0.1.8.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for rector-0.1.8.tar.gz
Algorithm Hash digest
SHA256 fd695b1fa69aa37262d6e9efeef030ed656a44732d7769c9795b28595c1aa36b
MD5 6d44fccb8d45516825bcf3fd0b91fa06
BLAKE2b-256 31a60e4ec7b2347b04812fda22384c6ec431559b6cdaba670eef7d113a74d8e3

See more details on using hashes here.

File details

Details for the file rector-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: rector-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 13.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for rector-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6f89759f2379982b9f714e1a9e890dcb4e9fd0d3d6eaf6514441b7d3ab1fad22
MD5 8f3b5f0f2a0f64d1ec4d9d34026fd365
BLAKE2b-256 e2507dcffb9bcf34d769077ae3334bc483e8eedcc519b3714969c5be0c31982f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page