Skip to main content

Easily build Enum-like regular expression patterns

Project description

renum

pypi Licensed under the MIT License Ruff pre-commit.ci status

Regex Enum

A utility class for generating Enum-like regular expression patterns.

Installing

python3 -m pip install -U renum

Development version:

python3 -m pip install -U https://github.com/Zephyrkul/renum/archive/master.zip#egg=renum

Support

If you need help with using renum, find a bug, or have a feature request, feel free to file an issue.

Examples

Parsing from standard input:

import regex
from renum import renum


class Actions(renum, flags=regex.IGNORECASE):
    GO = r"go (?P<direction>north|south|east|west)"
    EXAMINE = r"examine (?P<item>[\w\s]+)"
    OPEN = r"open (?P<object>door|chest)"


if __name__ == "__main__":
    while True:
        line = input()
        if not line:
            break
        action = Actions.match(line)  # The renum class acts like a Pattern...
        if action is Actions.GO:
            print("You went %s" % action.group("direction"))  # and each entry acts like a Match
        elif action is Actions.EXAMINE:
            print("You take a closer look at %s. Looks grungy." % action.group("item"))
        elif action is Actions.OPEN:
            print("You tried to open the %s, but it was locked." % action.group("object"))
        else:
            print("Unknown action: %s" % line)

Troubleshooting a misbehaving renum:

>>> import regex
>>> from renum import renum
>>>
>>> class Bad(renum, flags=regex.IGNORECASE | regex.DEBUG):
...     GOOD = r"no (?:issues|problems) here"
...     BAD = r"whoops,\s(?P<missed something)"
...
regex.error: bad character in group name at position 29 in BAD
whoops,\s(?P<missed something)
                             ^

Requirements

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

renum-0.0.1b1.tar.gz (10.2 kB view hashes)

Uploaded Source

Built Distribution

renum-0.0.1b1-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page