Skip to main content

Write regular expressions like poetry in Python - transform cryptic regex into elegant, readable patterns

Project description

RePoet: Write Regular Expressions Like Poetry in Python

PyPI version License: MIT

🎯 Transform regex into poetry! Write regular expressions as elegantly as writing verses.

✨ Highlights

from repoet import op

# Traditional regex (cryptic spell)
date_regex = r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$"

# With RePoet (elegant verse)
date = op.seq(
    op.begin,
    op.group(op.digit * 4, name="year") + "-",
    op.group(op.digit * 2, name="month") + "-",
    op.group(op.digit * 2, name="day"),
    op.end
)

match = date.match("2024-03-01")
print(match.group("year"))   # "2024"
print(match.group("month"))  # "03"
print(match.group("day"))    # "01"

🚀 Why RePoet?

  • 🎭 Full Re API Compatibility - All re module features are supported
  • 🎨 Operator Magic - Use +, |, * to compose patterns naturally
  • 📝 Multiple Styles - Choose between operator style or functional style
  • 🛡️ Type-Safe - Full type hints for better IDE support
  • 🎯 Zero Learning Curve - If you know regex, you know RePoet

💫 Quick Start

pip install repoet

📖 Core Concepts

Pattern Composition

from repoet import op

# Using operators
pattern = op.digit + op.word + op.space    # \d\w+\s
pattern = op.digit | op.word               # (?:\d|\w+)
pattern = op.digit * 3                     # \d{3}

# Using functions
pattern = op.seq(op.digit, op.word, op.space)
pattern = op.alt(op.digit, op.word)
pattern = op.times(3)(op.digit)

Named Groups & Captures

# Match phone numbers with named groups
phone = op.seq(
    op.maybe("+"),
    op.group(op.digit * 2, "country"),
    " ",
    op.group(op.digit * 3, "area"),
    "-",
    op.group(op.digit * 4, "number")
)

match = phone.match("+86 123-4567")
print(match.group("country"))  # "86"
print(match.group("number"))   # "4567"

Advanced Features

# Lookarounds
price = op.behind("$") + op.digit * 2      # (?<=\$)\d{2}
not_end = op.word + op.not_ahead(op.end)   # \w+(?!$)

# Character Classes
username = op.some(op.anyof("a-zA-Z0-9_"))  # [a-zA-Z0-9_]+
not_digit = op.exclude("0-9")               # [^0-9]

# Quantifiers
optional = op.maybe("s")                    # s?
one_plus = op.some(op.letter)               # \w+
any_amount = op.mightsome(op.space)         # \s*

🎯 Pattern API

RePoet patterns support all standard re module methods:

pattern = op.word + "@" + op.word

# All re module methods are available
pattern.match(string)
pattern.search(string)
pattern.findall(string)
pattern.finditer(string)
pattern.sub(repl, string)
pattern.split(string)

📚 More Examples

URL Parser

url = op.seq(
    op.group(op.alt("http", "https"), "protocol"),
    "://",
    op.group(op.some(op.anyof("a-z0-9.-")), "domain"),
    op.group(op.mightsome(op.anyof("/a-z0-9.-")), "path")
)

Date Validator

date = (op.digit * 4) + "-" + \
       (op.digit * 2) + "-" + \
       (op.digit * 2)

🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest features
  • Submit pull requests

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.


⭐️ If you find RePoet useful, please star it on GitHub! ⭐️

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

repoet-0.1.1.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

repoet-0.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file repoet-0.1.1.tar.gz.

File metadata

  • Download URL: repoet-0.1.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for repoet-0.1.1.tar.gz
Algorithm Hash digest
SHA256 db120b0407bfc5c48d063617c74994db613fe4e60da1b7f1579fca938701e33a
MD5 b5ad10c26eaf5e1b1804fae4a84eae2c
BLAKE2b-256 eb6c7d473e4abfb7731f66d0fc65915b56854c945443028ee66f31c4dbadeb85

See more details on using hashes here.

File details

Details for the file repoet-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: repoet-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for repoet-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40e2c0414b92f4dccccafab4b63dea740ffd9d20d5a662ef99a37eeccec98cde
MD5 a5a50c9568d8a60868f19bd12cb00e70
BLAKE2b-256 42ff694f09a2f83f668ced5a6152cdf9e9f81436e948089ac30782b30fe9d7d7

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