1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
========
Edify
========
.. Cover Image
.. image:: https://raw.githubusercontent.com/luciferreeves/edify/main/images/cover.png
:alt: Cover Image
|
.. image:: https://readthedocs.org/projects/edify/badge/?style=flat&version=latest
:target: https://edify.readthedocs.io/
:alt: Documentation Status
.. image:: https://github.com/luciferreeves/edify/actions/workflows/github-actions.yml/badge.svg?branch=main
:alt: GitHub Actions Build Status
:target: https://github.com/luciferreeves/edify/actions
.. image:: https://codecov.io/gh/luciferreeves/edify/branch/main/graphs/badge.svg?branch=main
:alt: Coverage Status
:target: https://codecov.io/github/luciferreeves/edify
.. image:: https://img.shields.io/pypi/v/edify.svg
:alt: PyPI Package latest release
:target: https://pypi.org/project/edify
.. image:: https://img.shields.io/pypi/wheel/edify.svg
:alt: PyPI Wheel
:target: https://pypi.org/project/edify
.. image:: https://img.shields.io/pypi/pyversions/edify.svg
:alt: Supported versions
:target: https://pypi.org/project/edify
.. image:: https://img.shields.io/pypi/implementation/edify.svg
:alt: Supported implementations
:target: https://pypi.org/project/edify
.. image:: https://img.shields.io/github/commits-since/luciferreeves/edify/v0.2.1.svg
:alt: Commits since latest release
:target: https://github.com/luciferreeves/edify/compare/v0.2.1...main
.. end-badges
|
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``:
.. code-block:: bash
pip install edify
You can also install the in-development version with:
.. code-block:: bash
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 protected]``.
.. code-block:: python
from edify.library import email
email_addr = "[email protected]"
assert email(email_addr) == True
Building Regex Example
----------------------
The following example recognises and captures the value of a 16-bit hexadecimal number like ``0xC0D3``.
.. code-block:: python
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 <https://edify.readthedocs.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 [#f1]_. The API uses the `fluent builder pattern <https://en.wikipedia.org/wiki/Fluent_interface>`_, 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.
- ``subexpressions`` can 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!
.. _SuperExpressive: https://github.com/francisrstokes/super-expressive
.. [1]:
License & Contributing
======================
This project is licensed under `Apache Software License 2.0 <https://github.com/luciferreeves/edify/blob/main/LICENSE>`_. See `Contributing Guidelines <https://github.com/luciferreeves/edify/blob/main/CONTRIBUTING.rst>`_ for information on how to contribute to this project.
Contributors
------------
.. image:: https://contrib.rocks/image?repo=luciferreeves/edify
.. rubric:: Footnotes
.. [#f1] ``RegexBuilder`` class based on the `SuperExpressive`_ library.
|