{"id":17334846,"url":"https://github.com/tfeldmann/simplematch","last_synced_at":"2025-04-05T04:08:41.413Z","repository":{"id":45368765,"uuid":"358254665","full_name":"tfeldmann/simplematch","owner":"tfeldmann","description":"Minimal, super readable string pattern matching for python.","archived":false,"fork":false,"pushed_at":"2024-06-01T05:24:11.000Z","size":113,"stargazers_count":181,"open_issues_count":5,"forks_count":8,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-29T03:09:19.080Z","etag":null,"topics":["pattern-matching","python","regex","regular-expressions","string-matching"],"latest_commit_sha":null,"homepage":"","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/tfeldmann.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"tfeldmann","patreon":null,"open_collective":null,"ko_fi":"tfeldmann","tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["paypal.me/tfeldmann42"]}},"created_at":"2021-04-15T12:40:06.000Z","updated_at":"2024-11-16T00:12:32.000Z","dependencies_parsed_at":"2024-01-02T00:52:39.756Z","dependency_job_id":"b310151c-17e6-4adb-9411-bd6c91ac1e35","html_url":"https://github.com/tfeldmann/simplematch","commit_stats":{"total_commits":54,"total_committers":3,"mean_commits":18.0,"dds":"0.12962962962962965","last_synced_commit":"748fb9b5d534fe05ab3d8e63fa10e2c23caa1117"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tfeldmann%2Fsimplematch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tfeldmann%2Fsimplematch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tfeldmann%2Fsimplematch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tfeldmann%2Fsimplematch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tfeldmann","download_url":"https://codeload.github.com/tfeldmann/simplematch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247284944,"owners_count":20913704,"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":["pattern-matching","python","regex","regular-expressions","string-matching"],"created_at":"2024-10-15T15:07:20.775Z","updated_at":"2025-04-05T04:08:41.391Z","avatar_url":"https://github.com/tfeldmann.png","language":"Python","funding_links":["https://github.com/sponsors/tfeldmann","https://ko-fi.com/tfeldmann","paypal.me/tfeldmann42"],"categories":[],"sub_categories":[],"readme":"\u003cimg width=\"500\" src=\"https://raw.githubusercontent.com/tfeldmann/simplematch/main/docs/simplematch.svg\" alt=\"logo\"\u003e\n\n# simplematch\n\n\u003e Minimal, super readable string pattern matching for python.\n\n[![PyPI Version][pypi-image]][pypi-url]\n![PyPI - License](https://img.shields.io/pypi/l/simplematch)\n[![tests](https://github.com/tfeldmann/simplematch/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/tfeldmann/simplematch/actions/workflows/tests.yml)\n\n```python\nimport simplematch\n\nsimplematch.match(\"He* {planet}!\", \"Hello World!\")\n\u003e\u003e\u003e {\"planet\": \"World\"}\n\nsimplematch.match(\"It* {temp:float}°C *\", \"It's -10.2°C outside!\")\n\u003e\u003e\u003e {\"temp\": -10.2}\n```\n\n## Installation\n\n`pip install simplematch`\n\n(Or just drop the `simplematch.py` file in your project.)\n\n## Syntax\n\n`simplematch` has only two syntax elements:\n\n- wildcard `*`\n- capture group `{name}`\n\nCapture groups can be named (`{name}`), unnamed (`{}`) and typed (`{name:float}`).\n\nThe following types are available:\n\n- `int`\n- `float`\n- `email`\n- `url`\n- `ipv4`\n- `ipv6`\n- `bitcoin`\n- `ssn` (social security number)\n- `ccard` (matches Visa, MasterCard, American Express, Diners Club, Discover, JCB)\n\nFor now, only named capture groups can be typed.\n\nThen use one of these functions:\n\n```python\nimport simplematch\n\nsimplematch.match(pattern, string) # -\u003e returns a `dict` on match, `None` otherwise.\nsimplematch.test(pattern, string)  # -\u003e returns `True` on match, `False` otherwise.\n```\n\nOr use a `Matcher` object:\n\n```python\nimport simplematch as sm\n\nmatcher = sm.Matcher(pattern)\n\nmatcher.match(string) # -\u003e returns a dict or None\nmatcher.test(string)  # -\u003e returns True / False\nmatcher.regex         # -\u003e shows the generated regex\n```\n\n## Basic usage\n\n```python\nimport simplematch as sm\n\n# extracting data\nsm.match(\n    pattern=\"Invoice_*_{year}_{month}_{day}.pdf\",\n    string=\"Invoice_RE2321_2021_01_15.pdf\")\n\u003e\u003e\u003e {\"year\": \"2021\", \"month\": \"01\", \"day\": \"15\"}\n\n# test match only\nsm.test(\"ABC-{value:int}\", \"ABC-13\")\n\u003e\u003e\u003e True\n```\n\n## Typed matches\n\n```python\nimport simplematch as sm\n\nmatcher = sm.Matcher(\"{year:int}-{month:int}: {value:float}\")\n\n# extracting data\nmatcher.match(\"2021-01: -12.786\")\n\u003e\u003e\u003e {\"year\": 2021, \"month\": 1, \"value\": -12.786}\n\n# month is no integer -\u003e no match and return `None`.\nmatcher.match(\"2021-AB: Hello\")\n\u003e\u003e\u003e None\n\n# no extraction, only test for match\nmatcher.test(\"1234-01: 123.123\")\n\u003e\u003e\u003e True\n\n# show generated regular expression\nmatcher.regex\n\u003e\u003e\u003e '^(?P\u003cyear\u003e[+-]?[0-9]+)\\\\-(?P\u003cmonth\u003e[+-]?[0-9]+):\\\\ (?P\u003cvalue\u003e[+-]?(?:[0-9]*[.])?[0-9]+)$'\n\n# show registered converters\nmatcher.converters\n\u003e\u003e\u003e {'year': \u003cclass 'int'\u003e, 'month': \u003cclass 'int'\u003e, 'value': \u003cclass 'float'\u003e}\n```\n\n## Register your own types\n\nYou can register your own types to be available for the `{name:type}` matching syntax\nwith the `register_type` function.\n\n`simplematch.register_type(name, regex, converter=str)`\n\n- `name` is the name to use in the matching syntax\n- `regex` is a regular expression to match your type\n- `converter` is a callable to convert a match (`str` by default)\n\n### Example\n\nRegister a `smiley` type to detect smileys (`:)`, `:(`, `:/`) and getting their moods:\n\n```python\nimport simplematch as sm\n\ndef mood_convert(smiley):\n    moods = {\n        \":)\": \"good\",\n        \":(\": \"bad\",\n        \":/\": \"sceptic\",\n    }\n    return moods.get(smiley, \"unknown\")\n\nsm.register_type(\"smiley\", r\":[\\)\\(\\/]\", mood_convert)\n\nsm.match(\"I'm feeling {mood:smiley} *\", \"I'm feeling :) today!\")\n\u003e\u003e\u003e {\"mood\": \"good\"}\n```\n\n## CLI Command\n\nYou can also install `simplematch` for use as a CLI command e.g. using `pipx`.\n\n```sh\npipx install simplematch\n```\n\n### Usage\n\n```sh\nusage: simplematch [-h] [--regex] pattern [strings ...]\n\npositional arguments:\n  pattern     A matching pattern\n  strings     The string to match\n\noptions:\n  -h, --help  show this help message and exit\n  --regex     Show the generated regular expression\n```\n\n### Example\n\nExtract a date from a specific file name:\n\n```sh\nsimplematch \"Invoice_*_{year}_{month}_{day}.pdf\" \"Invoice_RE2321_2021_01_15.pdf\"\n\u003e\u003e\u003e {\"year\": \"2021\", \"month\": \"01\", \"day\": \"15\"}\n```\n\n## Background\n\n`simplematch` aims to fill a gap between parsing with `str.split()` and regular\nexpressions. It should be as simple as possible, fast and stable.\n\nThe `simplematch` syntax is transpiled to regular expressions under the hood, so\nmatching performance should be just as good.\n\nI hope you get some good use out of this!\n\n## Contributions\n\nContributions are welcome! Just submit a PR and maybe get in touch with me via email\nbefore big changes.\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n\u003c!-- Badges --\u003e\n\n[pypi-image]: https://img.shields.io/pypi/v/simplematch\n[pypi-url]: https://pypi.org/project/simplematch/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftfeldmann%2Fsimplematch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftfeldmann%2Fsimplematch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftfeldmann%2Fsimplematch/lists"}