Skip to main content

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.

Release files for rector 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rector 0.1.9
File Size Uploaded
rector-0.1.9.tar.gz 17.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rector 0.1.9
File Interpreter ABI Platform
rector-0.1.9-py3-none-any.whl Python 3 none any Details

Total release size: 30.6 kB

Release files / rector-0.1.9.tar.gz

Download URL rector-0.1.9.tar.gz
Size 17.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b64e57a41dd912c1e98ad59a2f4a6cd7ab094481a5bfa0685601b8e2eb9fae43
BLAKE2b-256 checksum
How to use checksums
76c558bfef3adece1dbe831920c2fa23244f2adac8c9b08863da2c09d39a5920
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release files / rector-0.1.9-py3-none-any.whl

Download URL rector-0.1.9-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00cd65ec0da4835c7a71315e6916e504be97d79db3f3246e2305b2665e7c5014
BLAKE2b-256 checksum
How to use checksums
55f3b73c422e84aff79910e7be0e006ca2c24b0ee7c2c622ae4c65c215ded576
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.3

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page