Skip to content

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

License

Notifications You must be signed in to change notification settings

Zephyrkul/renum

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages