Skip to main content
logo

simplematch

Minimal, super readable string pattern matching for python.

PyPI Version PyPI - License tests

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

(Or just drop the simplematch.py file in your project.)

Syntax

simplematch has only two syntax elements:

  • wildcard *
  • capture group {name}

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

The following types are available:

  • int
  • 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` on match, `None` otherwise.
simplematch.test(pattern, string)  # -> returns `True` on match, `False` otherwise.

Or use a Matcher object:

import simplematch as sm

matcher = sm.Matcher(pattern)

matcher.match(string) # -> returns a dict or None
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

Typed matches

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 and return `None`.
matcher.match("2021-AB: Hello")
>>> None

# 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]+)$'

# show registered converters
matcher.converters
>>> {'year': <class 'int'>, 'month': <class 'int'>, 'value': <class 'float'>}

Register your own types

You can register your own types to be available for the {name:type} matching syntax with the register_type function.

simplematch.register_type(name, regex, converter=str)

  • name is the name to use in the matching syntax
  • regex is a regular expression to match your type
  • converter is a callable to convert a match (str by default)

Example

Register a smiley type to detect smileys (:), :(, :/) and getting their moods:

import simplematch as sm

def mood_convert(smiley):
    moods = {
        ":)": "good",
        ":(": "bad",
        ":/": "sceptic",
    }
    return moods.get(smiley, "unknown")

sm.register_type("smiley", r":[\)\(\/]", mood_convert)

sm.match("I'm feeling {mood:smiley} *", "I'm feeling :) today!")
>>> {"mood": "good"}

CLI Command

You can also install simplematch for use as a CLI command e.g. using pipx.

pipx install simplematch

Usage

usage: simplematch [-h] [--regex] pattern [strings ...]

positional arguments:
  pattern     A matching pattern
  strings     The string to match

options:
  -h, --help  show this help message and exit
  --regex     Show the generated regular expression

Example

Extract a date from a specific file name:

simplematch "Invoice_*_{year}_{month}_{day}.pdf" "Invoice_RE2321_2021_01_15.pdf"
>>> {"year": "2021", "month": "01", "day": "15"}

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

Metadata

Release files for simplematch 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simplematch 1.4
File Size Uploaded
simplematch-1.4.tar.gz 5.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for simplematch 1.4
File Interpreter ABI Platform
simplematch-1.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / simplematch-1.4.tar.gz

Download URL simplematch-1.4.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
55a77278b3d0686cb38e3ffe5a326a5f59c2995f1ba1fa1a4f68872c17caf4cb
BLAKE2b-256 checksum
How to use checksums
d4c5209aa49f6c366f5b1d80e9eef2f75270079df3c9dec4658e0716e4bcd6ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.5 Darwin/22.6.0

Release files / simplematch-1.4-py3-none-any.whl

Download URL simplematch-1.4-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e7b898e174bc11c3bddc1b1ee36a9d70dd96037295837a879195052c92107237
BLAKE2b-256 checksum
How to use checksums
43092522a9249284657b80c35b5a06fda30d466f1065d387c05c0c7cf0bf4892
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.5 Darwin/22.6.0

Release history Release notifications | RSS feed

This release

1.4 This release

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page