Skip to main content

A more pythonic extension of the builtin `re` module.

Project description

regexonic

A more Pythonic extension of Python's builtin re module.

import regexonic as re

Serves as a drop-in replacement for the builtin re library—all standard re functions, constants, and classes are re-exported.

Installation

pip install regexonic

OR

uv add regexonic

Purpose

The standard re module works well, but has some rough edges:

  • Methods return None on failure, requiring constant null checks
  • No built-in way to raise exceptions on match failure
  • Type hints could be more precise for IDE support
  • Common patterns like "validate or raise" require boilerplate

regexonic provides a thin wrapper that addresses these issues while maintaining full compatibility with the standard library.

Usage

import regexonic as re

# Create a pattern using the helper function
email = re.define(r"[\w.-]+@[\w.-]+\.\w+")

# Or use the class directly
email = re.Expression(r"[\w.-]+@[\w.-]+\.\w+")

# Boolean checking
email.check("user@example.com")  # True
email.check("invalid-email")     # False

# Get match or None (standard behavior)
email.match("user@example.com")  # <re.Match object>
email.match("invalid-email")     # None

# Get match or raise exception
email.match("user@example.com", required=True)  # <re.Match object>
email.match("invalid-email", required=True)     # Raises MatchNotFoundError

# Validate and return the string
email.validate("user@example.com")  # "user@example.com"
email.validate("invalid-email")     # Raises ValueError

Core Classes

Expression

A drop-in replacement for re.Pattern with enhanced functionality.

import regexonic as re

pattern = re.Expression(r"\d{3}-\d{4}", flags=re.IGNORECASE)

# All standard re.Pattern methods are available
pattern.search("Call 555-1234 today")
pattern.findall("555-1234 or 555-5678")
pattern.sub("XXX-XXXX", "Call 555-1234")
pattern.split("a-555-1234-b")

# Enhanced methods with `required` parameter
pattern.search("no numbers here", required=True)  # Raises SearchNotFoundError
pattern.match("555-1234", required=True)          # Returns match or raises MatchNotFoundError

# Convenience methods
pattern.check("555-1234")              # True (full match by default)
pattern.check("555-1234", full=False)  # True (partial match)
pattern.test("555-1234")               # Alias for check()
pattern.validate("555-1234")           # Returns string or raises ValueError

Structure

A base class for creating reusable pattern classes with custom methods.

import regexonic as re

class PhoneNumber(re.Structure):
    compiled = re.compile(r"(?P<area>\d{3})-(?P<exchange>\d{3})-(?P<number>\d{4})")
    
    @classmethod
    def parse(cls, string: str) -> dict[str, str]:
        match = cls.fullmatch(string, required=True)
        return match.groupdict()
    
    @classmethod
    def format(cls, string: str) -> str:
        groups = cls.parse(string)
        return f"({groups['area']}) {groups['exchange']}-{groups['number']}"

PhoneNumber.check("555-123-4567")      # True
PhoneNumber.parse("555-123-4567")      # {'area': '555', 'exchange': '123', 'number': '4567'}
PhoneNumber.format("555-123-4567")     # "(555) 123-4567"

Matcher

A lightweight callable class optimized for boolean matching.

import regexonic as re

is_hex_color = re.Matcher(r"#[0-9A-Fa-f]{6}")

is_hex_color("#FF5733")  # True
is_hex_color("red")      # False

# Get the actual match object when needed
is_hex_color.get("#FF5733")                  # <re.Match object>
is_hex_color.get("red", required=True)       # Raises MatchNotFoundError

Subber

A callable wrapper for substitution operations.

import regexonic as re

censor = re.Subber("***", r"\b(bad|ugly|evil)\b", flags=re.IGNORECASE)

censor("That was a bad idea")   # "That was a *** idea"
censor("Bad BAD bad", count=2)  # "*** *** bad"
censor.n("Bad BAD bad")         # ("*** *** ***", 3) - returns count too

Exception Handling

regexonic provides descriptive exceptions when matches fail:

import regexonic as re

pattern = re.define(r"\d+")

try:
    pattern.match("no digits", required=True)
except re.MatchNotFoundError as e:
    print(e.pattern)   # The regex pattern
    print(e.input)     # The input string
    print(e.detail)    # Human-readable message

Type Safety

All classes are generic and support both str and bytes patterns:

import regexonic as re

# String patterns
str_pattern: re.Expression[str] = re.Expression(r"\w+")
str_pattern.match("hello")  # Match[str]

# Bytes patterns
bytes_pattern: re.Expression[bytes] = re.Expression(rb"\w+")
bytes_pattern.match(b"hello")  # Match[bytes]

API Reference

Expression Methods

Method Description
search(string, *, required=False) Search for pattern anywhere in string
match(string, *, full=False, required=False) Match at beginning of string
fullmatch(string, *, required=False) Match entire string
findall(string) Return all non-overlapping matches
finditer(string) Return iterator of match objects
split(string, maxsplit=0) Split string by pattern
sub(repl, string, count=0) Replace matches with replacement
subn(repl, string, count=0) Replace and return count
check(string, full=True) Return True if pattern matches
test(string, full=True) Alias for check()
validate(string, full=True) Return string or raise ValueError

Re-exported from re

All standard re module exports are available:

  • Functions: compile, search, match, fullmatch, split, findall, finditer, sub, subn, escape, purge
  • Constants: ASCII/A, IGNORECASE/I, LOCALE/L, MULTILINE/M, DOTALL/S, UNICODE/U, VERBOSE/X, NOFLAG
  • Classes: Pattern, Match, RegexFlag, error

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

regexonic-0.1.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

regexonic-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file regexonic-0.1.0.tar.gz.

File metadata

  • Download URL: regexonic-0.1.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.4

File hashes

Hashes for regexonic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 92ad8ed17caefea5a1335dfd5518dde5946a94f9b1c8fbcc92dc143d0a058d61
MD5 43a50cc3de62dfa7ae1b2415769912d0
BLAKE2b-256 2e4818700ae9e8ef4b87e3b9b17846340cc88ffc6f5db10b27c6a07930ab0741

See more details on using hashes here.

File details

Details for the file regexonic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: regexonic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.4

File hashes

Hashes for regexonic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2368cd634d214d0d947aa727087cca3c2e538edd800429715a0a807da5e0ca58
MD5 2d394d651bfdbbb98bca8716664ab53a
BLAKE2b-256 8f419cd3f0fd3c2e9ee19fdb1f1f519fbce6a8805166d969fbc65366ecd49b33

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