Intuitive string pattern matching:
```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}
```
Based on the "Parser" category.
Alternatively, view simplematch alternatives based on common mentions on social networks and blogs.
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of simplematch or a related project?
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}
pip install simplematch
(Or just drop the simplematch.py file in your project.)
simplematch has only two syntax elements:
*{name}Capture groups can be named ({name}), unnamed ({}) and typed ({name:float}).
The following types are available:
intfloatemailurlipv4ipv6bitcoinssn (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
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
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'>}
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 syntaxregex is a regular expression to match your typeconverter is a callable to convert a match (str by default)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"}
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 are welcome! Just submit a PR and maybe get in touch with me via email before big changes.
<!-- Badges -->
*Note that all licence references and agreements mentioned in the simplematch README section above
are relevant to that project's source code only.
Do not miss the trending, packages, news and articles with our weekly report.