Skip to main content

A simple, object-oriented, regular expression generator.

Project description

ooregex

GitHub release (latest SemVer) PyPI - Downloads GitHub Code Style - Black

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("$£€")
        ),
)
# (?P<price>\d+(?:\.\d*)?)(?P<currency>[$£€])

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)
    # 9.99 £

Let's examine the pattern:

We have 2 groups:

  • a group named "price" consisting of:
    • one or more digits
    • optionally:
      • a dot
      • zero or more digits
  • a group named "currency" consisting of:
    • any character from "$£€"

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.

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

ooregex-0.2.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

ooregex-0.2.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file ooregex-0.2.0.tar.gz.

File metadata

  • Download URL: ooregex-0.2.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.4 Windows/10

File hashes

Hashes for ooregex-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6d48ff36b58f62846e9ef5072faf642e66a192c86d0ba7e553aebda575df566d
MD5 a0b2d2bfc0ed77dcaff8b06f00ada189
BLAKE2b-256 11bf8fbe3f72d3fd36b6802cdd87c3e51dfe7de37b24c9a26b20390b782d6834

See more details on using hashes here.

File details

Details for the file ooregex-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ooregex-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.4 Windows/10

File hashes

Hashes for ooregex-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8249c89068d59c28f82f28f3be067ae0b56b173b483432386497e7e4f71ada8c
MD5 b8e902e869b1584cf3f2d8cd7477de73
BLAKE2b-256 09d387973c49c610c317e8f6390aa5877a976dd24b6ffa5020f804035bfb456a

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