RePoet: Write Regular Expressions Like Poetry in Python
🎯 Transform regex into poetry! Write regular expressions as elegantly as writing verses.
✨ Highlights
from repoet import op
date_regex = r"^(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})$"
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"))
print(match.group("month"))
print(match.group("day"))
🚀 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
pattern = op.digit + op.word + op.space
pattern = op.digit | op.word
pattern = op.digit * 3
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
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"))
print(match.group("number"))
Advanced Features
price = op.behind("$") + op.digit * 2
not_end = op.word + op.not_ahead(op.end)
username = op.some(op.anyof("a-zA-Z0-9_"))
not_digit = op.exclude("0-9")
optional = op.maybe("s")
one_plus = op.some(op.letter)
any_amount = op.mightsome(op.space)
🎯 Pattern API
RePoet patterns support all standard re
module methods:
pattern = op.word + "@" + op.word
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! ⭐️