ooregex
A simple, object oriented, regular expression generator.
ooregex
is a package aimed at providing a simple syntax for composing
regular expressions, without having to memorise their syntax.
It does not guarantee that the expressions generated are the most efficient.
It is assumed that users have some understanding of regular expressions, as there's nothing preventing invalid expressions from being generated.
This project most likely still needs more testing, so I won't bump it to version 1.0 until I'm sure it's good enough.
Installation
You can install this package using pip with the command
pip install ooregex
Usage
The main purpose of this package is generating regular expressions to be used in other projects, for example, with the built-in re module.
See the full documentation here.
Import the module with:
import ooregex
Alternatively, import only the symbols that you need:
from ooregex import (...)
Now let's build an expression for matching a price tag:
import re
from ooregex import *
pattern = Regex(
Group(name="price", expression=Regex(
DIGIT[1:],
Optional(DOT + DIGIT[:]))
),
Group(name="currency", expression=
AnyOf("$£€")
),
)
test_str = "Sales! Everything for 9.99£!"
price_tag = re.search(str(pattern), test_str)
if price_tag is not None:
price = price_tag.group("price")
currency = price_tag.group("currency")
print(price, currency)
Let's examine the pattern:
We have 2 groups:
- a group named "price" consisting of:
- one or more digits
- optionally:
- a group named "currency" consisting of:
Look how much clearer it is compared to the generated string:
(?P<price>\d+(?:\.\d*)?)(?P<currency>[$£€])
Report a bug
If you find a bug, you can open an issue or email me.
License
This package is available under the MIT license.