{"id":24344402,"url":"https://github.com/xtream1101/regex-decorator","last_synced_at":"2025-04-09T17:15:19.327Z","repository":{"id":57460869,"uuid":"68349897","full_name":"xtream1101/regex-decorator","owner":"xtream1101","description":"Apply a decorator to a function that is triggered by a regex","archived":false,"fork":false,"pushed_at":"2017-03-15T20:45:26.000Z","size":15,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-09T17:15:12.665Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xtream1101.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-16T03:28:07.000Z","updated_at":"2021-11-22T23:47:38.000Z","dependencies_parsed_at":"2022-09-17T04:22:10.403Z","dependency_job_id":null,"html_url":"https://github.com/xtream1101/regex-decorator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtream1101%2Fregex-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtream1101%2Fregex-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtream1101%2Fregex-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xtream1101%2Fregex-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xtream1101","download_url":"https://codeload.github.com/xtream1101/regex-decorator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248074921,"owners_count":21043490,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-18T09:35:48.217Z","updated_at":"2025-04-09T17:15:19.294Z","avatar_url":"https://github.com/xtream1101.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Regex Decorator\n\n[![PyPI](https://img.shields.io/pypi/v/regex_decorator.svg)](https://pypi.python.org/pypi/regex_decorator)\n[![PyPI](https://img.shields.io/pypi/l/regex_decorator.svg)](https://pypi.python.org/pypi/regex_decorator)\n[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/ce0745991c4f49a0b9805d4cbeb10d2a)](https://www.codacy.com/app/eddy-hintze/regex-decorator?utm_source=github.com\u0026amp;utm_medium=referral\u0026amp;utm_content=xtream1101/regex-decorator\u0026amp;utm_campaign=Badge_Coverage)\n\nThis library allows you to place a decorator on a function which has a regex as an argument. Then when parsing text, if that regex is found it will run the function and optionally return data\n\n\n## Install\n```\n$ pip3 install regex_decorator\n```\n\n## Example\n\nCan use `re` or `parse` in the decorator\n\nDefault for `parse_using` is `parse`\n\n`@p.listener('my name is (\\w+).', re.IGNORECASE, parse_using='re')`\n\n`@p.listener('my name is (?P\u003cname\u003e\\w+).', re.IGNORECASE, parse_using='re')`\n\n`@p.listener('my name is {}.')`\n\n`@p.listener('my name is {name}.')`\n\nBoth of the above will match `Eddy` with the input of `my name is Eddy.`\n\n```python\n# Content of test_strings.txt\n# Foo 1\n# Don't match this line\n# maybe this line because the answer is 99\n# Hi, my name is Eddy!\n# the answer is 44, foo 4\n\nimport re\nimport logging\nfrom regex_decorator import Parser\n\nlogging.basicConfig(level=logging.INFO,\n                    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n\nlogger = logging.getLogger(__name__)\n\np = Parser()\n\n\n@p.listener('my name is (\\w+)', re.IGNORECASE, parse_using='re')\ndef name(matched_str, name):\n    return name\n\n\n@p.listener('The answer is (\\d+)', re.IGNORECASE, parse_using='re')\ndef answer(matched_str, answer):\n    return answer\n\n\n@p.listener('foo (\\d)', re.IGNORECASE, parse_using='re')\ndef foo(matched_str, value):\n    return value\n\n\n@p.listener('What is (?P\u003cval1\u003e\\d+) \\+ (?P\u003cval2\u003e\\d+)', re.IGNORECASE, parse_using='re')\ndef add(matched_str, val2, val1):\n    \"\"\"\n    When using named args in the regex `(?P\u003cname\u003e)`, the order of the args does not matter\n    \"\"\"\n    ans = int(val1) + int(val2)\n    print(val1, \"+\", val2, \"=\", ans)\n\n\nexample1 = p.parse(\"My name is Eddy, and the answer is 42.\")\nprint(example1)  # Returns: Eddy\n\nexample2 = p.parse_all(\"My name is Eddy, and the answer is 42.\")\nprint(example2)  # Returns: ['Eddy', '42']\n\nexample3 = p.parse_file('test_strings.txt')\nprint(example3)  # Returns: ['1', '99', 'Eddy', '44']\n\nexample4 = p.parse_file_all('test_strings.txt')\nprint(example4)  # Returns: ['1', '99', 'Eddy', '44', '4']\n\n# It does not always have to return something and all action can be completed in the function like so:\np.parse(\"what is 2 + 3\")  # Prints: 2 + 3 = 5\n\n# Reference here for more examples: https://github.com/xtream1101/regex-decorator/blob/master/test_parsing.py\n\n```\n\n## Example Use case\nUse it with a speach to text library and create custom home automation commands.\nThis example requires `speech_recognition`\n\n`$ pip3 install speech_recognition`\n\n```python\nimport re\nimport speech_recognition as sr\nfrom regex_decorator import Parser\n\np = Parser()\n\n\n@p.listener('Turn (?P\u003caction\u003e\\w+) (?:my|the)?\\s?(?P\u003citem\u003e.+)', re.IGNORECASE)\ndef on(matched_str, action, item):\n    # Some home automation action here\n    print(\"Turning\", action, item)\n\n\n# Try and say:\n#   \"Turn on living room lights\"\n#   \"Turn off the living room lights\"\n\n# obtain audio from the microphone\nr = sr.Recognizer()\nwith sr.Microphone() as source:\n    print(\"Say something!\")\n    audio = r.listen(source)\n\n# recognize speech using Google Speech Recognition\ntry:\n    result = r.recognize_google(audio)\n    print(\"Google Speech Recognition thinks you said \" + result)\n    p.parse(result)\nexcept sr.UnknownValueError:\n    print(\"Google Speech Recognition could not understand audio\")\nexcept sr.RequestError as e:\n    print(\"Could not request results from Google Speech Recognition service; {0}\".format(e))\n```\n\n## Run tests\nJust run the command `pytest` in the root directory (may need to `pip3 install pytest`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtream1101%2Fregex-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxtream1101%2Fregex-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxtream1101%2Fregex-decorator/lists"}