• Stars
    star
    175
  • Rank 216,768 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimal, super readable string pattern matching for python.

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"}

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

More Repositories

1

organize

The file management automation tool.
Python
2,031
star
2

gpsdclient

A simple gpsd client and python library.
Python
54
star
3

Arduino-Blinkenlight

Non-blocking fading patterns for single LEDs.
C++
32
star
4

fretty.app

An interactive fretboard exploration tool for all stringed instruments.
Vue
32
star
5

Sudoku

Use your webcam to detect and solve any 9x9 sudoku
Python
26
star
6

python-raumfeld

A pythonic library for discovering and controlling Teufel Raumfeld devices.
Python
23
star
7

Raumfeld-Desktop

Desktop Control Software for Teufel Raumfeld Loudspeakers
Python
12
star
8

Arduino-Timeout

Minimal, production-ready timeout library for Arduino. With pause / resume.
C++
11
star
9

tryagain

A simple and pythonic retry helper for calling unstable functions
Python
10
star
10

Arduino-MotorDriver

Generic Arduino interface for various motor drivers.
C++
5
star
11

Arduino-SchmittTrigger

A lightweight schmitt trigger for debouncing and filtering.
C++
5
star
12

QlockToo

A QlockTwo Remake
Python
4
star
13

Biorobotic-Arm

An Arduino controlled robotic arm with facetracking capabilities
Python
3
star
14

Thomdo

A simple PySide todo list
Python
3
star
15

font-awesome-png

All the font-awesome icons as png files
3
star
16

WordClock

A very thin word clock application written in QML
JavaScript
2
star
17

days360

Excel's DAYS360() formula for python.
Python
2
star
18

maze

A maze generator written in rust.
Rust
2
star
19

rust-schmitttrigger

Rust
1
star
20

MBWBTV-Sternefabrik

Kleines Geschicklichkeitsspiel für MBWBTV
Java
1
star
21

NagelSchreckenberg

Stausimulation
Java
1
star
22

MazeGen

Generate a maze with the help of a growing tree algorithm
Java
1
star
23

Baeckerei-Lensing

Website "Bäckerei Lensing"
PHP
1
star
24

Project-Euler

Python solutions for Project Euler
Python
1
star
25

Dijkstra

PySide Dijkstra Demo Application
Python
1
star
26

Vehicle-Evolution

Uses a genetic algorithm to create a working vehicle.
C
1
star
27

Arduino-Button

C++
1
star
28

supersigma2-errorcodes

A python parser for DMC SuperSigma2 error codes
Python
1
star
29

Vokabelcheck

Finds unknown vocabulary in latin texts
Python
1
star
30

TimeTableProblem

Generates a timetable for a small school
Prolog
1
star
31

Mecanum-Robot

Firmware and Simulator for controlling a vehicle with mecanum wheels
C++
1
star