Skip to main content

Minimal, super readable string pattern matching.

Project description

simplematch

Minimal, super readable string pattern matching for python.

PyPI Version

import simplematch

simplematch.match("He* {planet}!", "Hello World!")
>>> {"planet": "World"}

simplematch.match("It* {temp:float}°C *", "It's -10.2°C outside!")
>>> {"temp": -10.2}

Installation

pip install simplematch

Syntax

simplematch has only two syntax elements:

  • wildcard *
  • capture group {capture_name}

Capture groups can be named ({name}), unnamed ({*}) and typed ({name:float}).

The following types are available:

  • ìnt
  • float
  • email
  • url
  • ipv4
  • ipv6
  • bitcoin
  • ssn (social security number)
  • ccard (matches Visa, MasterCard, American Express, Diners Club, Discover, JCB)

For now, only named capture groups can be typed.

Then use one of these functions:

import simplematch

simplematch.match(pattern, string) # -> returns a dict
simplematch.test(pattern, string)  # -> return True / False

Or use a Matcher object:

import simplematch as sm

matcher = sm.Matcher(pattern)

matcher.match(string) # -> returns a dict
matcher.test(string)  # -> returns True / False
matcher.regex         # -> shows the generated regex

Basic usage

import simplematch as sm

# extracting data
sm.match(
    pattern="Invoice_*_{year}_{month}_{day}.pdf",
    string="Invoice_RE2321_2021_01_15.pdf")
>>> {"year": "2021", "month": "01", "day": "15"}

# test match only
sm.test("ABC-{value:int}", "ABC-13")
>>> True

Type hints

import simplematch as sm

matcher = sm.Matcher("{year:int}-{month:int}: {value:float}")

# extracting data
matcher.match("2021-01: -12.786")
>>> {"year": 2021, "month": 1, "value": -12.786}

# month is no integer, no match
matcher.match("2021-AB: Hello")
>>> {}

# no extraction, only test for match
matcher.test("1234-01: 123.123")
>>> True

# show generated regular expression
matcher.regex
>>> '^(?P<year>[+-]?[0-9]+)\\-(?P<month>[+-]?[0-9]+):\\ (?P<value>[+-]?(?:[0-9]*[.])?[0-9]+)$'

Background

simplematch aims to fill a gap between parsing with str.split() and regular expressions. It should be as simple as possible, fast and stable.

The simplematch syntax is transpiled to regular expressions under the hood, so matching performance should be just as good.

I hope you get some good use out of this!

Contributions

Contributions are welcome! Just submit a PR and maybe get in touch with me via email before big changes.

License

MIT

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

simplematch-1.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

simplematch-1.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file simplematch-1.0.tar.gz.

File metadata

  • Download URL: simplematch-1.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Darwin/18.7.0

File hashes

Hashes for simplematch-1.0.tar.gz
Algorithm Hash digest
SHA256 0913d36eef33e9a5946cb0d5e35e62a39340ebe3fd385b27a7fc0d8b3248247e
MD5 ca05c3d70b1d91712d315f61d9986394
BLAKE2b-256 46f27ae991f83e4db47568d717c2bc09bd07befa82be8f4b844cf38b97c138c8

See more details on using hashes here.

File details

Details for the file simplematch-1.0-py3-none-any.whl.

File metadata

  • Download URL: simplematch-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.4 Darwin/18.7.0

File hashes

Hashes for simplematch-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 182be170a960be726d50dd5b3dbf5101a3a12075c33b17e6dd13c3a8d5868a04
MD5 2fc5f6674596a66346bfaca26ac0cfe3
BLAKE2b-256 c9f4683b8acb8435d65a5d81bc1b9821d1ac013b6e5d97600cd0f1eaa4cc9569

See more details on using hashes here.

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