Skip to main content

Explain, test, and generate examples for regular expressions

Project description

rexplain

PyPI version PyPI - Python Version License: MIT

Explain, test, and generate examples for regular expressions.

Overview

rexplain is a Python toolkit for understanding, testing, and generating examples for regular expressions. It provides:

  • Human-readable, line-by-line explanations of regex patterns
  • Example string generation for any regex
  • Detailed match testing with feedback
  • Visual railroad diagrams for regex patterns
  • Both a Python API and a CLI

Features

  • Regex Explanation: Get clear, context-aware explanations for any regex pattern
  • Test Regex: Test if a string matches a pattern and see why/why not
  • Generate Examples: Generate example strings that match a regex
  • Visual Diagrams: Generate railroad diagrams to visualize regex patterns
  • CLI & API: Use from the command line or as a Python library
  • Regex Flags: Supports Python regex flags (e.g., re.IGNORECASE)

Installation

pip install rexplain

Quick Start

CLI Usage

Explain a regex pattern:

rexplain explain "^\d{3}-\d{2}-\d{4}$"

Generate example strings:

rexplain examples "[A-Za-z]{5}" --count 3

Test if a string matches a pattern:

rexplain test "^hello.*" "hello world!"

Generate a railroad diagram:

rexplain diagram "^\\w+$" --output diagram.svg
rexplain diagram "^\\w+$" --detailed --output detailed.svg

Python API Usage

from rexplain import explain, examples, test, diagram

print(explain(r"\d+"))
print(examples(r"[A-Z]{2}\d{2}", count=2))
print(test(r"foo.*", "foobar"))

# Generate diagrams
diagram(r"^\w+$", "simple.svg")
diagram(r"^\w+$", "detailed.svg", detailed=True)
svg_content = diagram(r"^\w+$")  # Returns SVG as string

Example: Detailed Explanation

from rexplain import explain
print(explain(r"abc\w+\w*10$"))
# Output:
# a - matches the character 'a' (ASCII 97) literally (case sensitive)
# b - matches the character 'b' (ASCII 98) literally (case sensitive)
# c - matches the character 'c' (ASCII 99) literally (case sensitive)
# \w+ - matches a word character one or more times (greedy)
# \w* - matches a word character zero or more times (greedy)
# 1 - matches the character '1' (ASCII 49) literally (case sensitive)
# 0 - matches the character '0' (ASCII 48) literally (case sensitive)
# $ - asserts position at the end of a line

API Reference

explain(pattern: str, flags: int = 0) -> str

Returns a line-by-line explanation of the regex pattern.

examples(pattern: str, count: int = 3, flags: int = 0) -> List[str]

Generates example strings that match the pattern.

test(pattern: str, test_string: str, flags: int = 0) -> dict

Tests if a string matches the pattern and explains why/why not.

diagram(pattern: str, output_path: str = None, detailed: bool = False) -> str

Generates a railroad diagram for the regex pattern. Returns SVG content or saves to file.

Contributing

Contributions are welcome! To contribute:

  • Fork the repo and create a branch
  • Add or improve features/tests/docs
  • Run tests
  • Open a pull request

Running Tests & Coverage

To run all tests with coverage (threshold: 90%):

pip install .[test]
pytest

If coverage is below 90%, pytest will fail. Coverage details will be shown in the terminal.

License

MIT

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

rexplain-0.3.3.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

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

rexplain-0.3.3-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file rexplain-0.3.3.tar.gz.

File metadata

  • Download URL: rexplain-0.3.3.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for rexplain-0.3.3.tar.gz
Algorithm Hash digest
SHA256 7a66f3ad2bead0c59c4ae0ff56853a6b01bcd7272dc04755bc844ee638f5de3a
MD5 1dcdd3766efb0f19a1d8827f4bca0b92
BLAKE2b-256 4efa848cc72c58351d143d4307442404344576577a0786d89011adc18b95aa94

See more details on using hashes here.

File details

Details for the file rexplain-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: rexplain-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.28.1

File hashes

Hashes for rexplain-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 43040ce0663bd255b3f23daeceec0587280fcba3b18c85b733777b162e2e098e
MD5 5b5b4397146a2910a5aabdaebc551fa5
BLAKE2b-256 57faf002ce55a7ca8e15ce33f0557c419a83e3d39df11fbbb927b7b3e0102c5a

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