Edify¶
Edify (/ˈɛdɪfaɪ/, “ed-uh-fahy”) is a Python library that allows you to easily create regular expressions for matching text in a programmatically-friendly way. It is designed to be used in conjunction with the re module.
It also allows you to verify a string quickly by providing commonly used regex patterns in its extensive set of built-in patterns. To tap into a pattern, simply import the pattern function from the edify.library module.
Quick Start¶
To get started make sure you have python 3.7 or later installed and then, install Edify from pip:
pip install edify
You can also install the in-development version with:
pip install https://github.com/luciferreeves/edify/archive/main.zip
Then go on to import the RegexBuilder class from the edify module.
Using Pre-Built Patterns¶
The following example recognises and captures any email like email@example.com.
from edify.library import email
email_addr = "email@example.com"
assert email(email_addr) == True
Building Regex Example¶
The following example recognises and captures the value of a 16-bit hexadecimal number like 0xC0D3.
from edify import RegexBuilder
expr = (
RegexBuilder()
.start_of_input()
.optional().string("0x")
.capture()
.exactly(4).any_of()
.range("A", "F")
.range("a", "f")
.range("0", "9")
.end()
.end()
.end_of_input()
.to_regex()
)
"""
Produces the following regular expression:
re.compile(^(?:0x)?([A-Fa-f0-9]{4})$)
"""
assert expr.match("0xC0D3")
Further Documentation¶
Further API documentation is available on edify.rftd.io.
Why Edify?¶
Regex is a powerful tool, but its syntax is not very intuitive and can be difficult to build, understand, and use. It gets even more difficult when you have to deal with backtracking, look-ahead, and other features that make regex difficult.
That’s where Edify becomes extremely useful. It allows you to create regular expressions in a programmatic way by invoking the RegexBuilder class 1. The API uses the fluent builder pattern, and is completely immutable. It is built to be discoverable and predictable.
Properties and methods describe what they do in plain English.
Order matters! Quantifiers are specified before the thing they change, just like in English (e.g.
RegexBuilder().exactly(5).digit()).If you make a mistake, you’ll know how to fix it. Edify will guide you towards a fix if your expression is invalid.
subexpressionscan be used to create meaningful, reusable components.
Edify turns those complex and unwieldy regexes that appear in code reviews into something that can be read, understood, and properly reviewed by your peers - and maintained by anyone!
License & Contributing¶
This project is licensed under Apache Software License 2.0. See Contributing Guidelines for information on how to contribute to this project.
Contributors¶
Footnotes
- 1
RegexBuilderclass based on the SuperExpressive library.