Package: simplematch
simplematchMinimal, super readable string pattern matching for python.importsimplematchsimplematch.match("He*{planet}!","Hello World!")>>>{"planet":"World"}simplematch.match("It* {temp:float}°C *","It's -10.2°C outside!")>>>{"temp":-10.2}Installationpip install simplematch(Or just drop thesimplematch.pyfile in your project.)Syntaxsimplematchhas only two syntax elements:wildcard*capture group{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:importsimplematchsimplematch.match(pattern,string)# -> returns a `dict` on match, `None` otherwise.simplematch.test(pattern,string)# -> returns `True` on match, `False` otherwise.Or use aMatcherobject:importsimplematchassmmatcher=sm.Matcher(pattern)matcher.match(string)# -> returns a dict or Nonematcher.test(string)# -> returns True / Falsematcher.regex# -> shows the generated regexBasic usageimportsimplematchassm# extracting datasm.match(pattern="Invoice_*_{year}_{month}_{day}.pdf",string="Invoice_RE2321_2021_01_15.pdf")>>>{"year":"2021","month":"01","day":"15"}# test match onlysm.test("ABC-{value:int}","ABC-13")>>>TrueTyped matchesimportsimplematchassmmatcher=sm.Matcher("{year:int}-{month:int}: {value:float}")# extracting datamatcher.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 matchmatcher.test("1234-01: 123.123")>>>True# show generated regular expressionmatcher.regex>>>'^(?P<year>[+-]?[0-9]+)\\-(?P<month>[+-]?[0-9]+):\\(?P<value>[+-]?(?:[0-9]*[.])?[0-9]+)$'# show registered convertersmatcher.converters>>>{'year':<class'int'>, 'month': <class 'int'>, 'value': <class 'float'>}Register your own typesYou can register your own types to be available for the{name:type}matching syntax
with theregister_typefunction.simplematch.register_type(name, regex, converter=str)nameis the name to use in the matching syntaxregexis a regular expression to match your typeconverteris a callable to convert a match (strby default)ExampleRegister asmileytype to detect smileys (:),:(,:/) and getting their moods:importsimplematchassmdefmood_convert(smiley):moods={":)":"good",":(":"bad",":/":"sceptic",}returnmoods.get(smiley,"unknown")sm.register_type("smiley",r":[\)\(\/]",mood_convert)sm.match("I'm feeling {mood:smiley} *","I'm feeling :) today!")>>>{"mood":"good"}CLI CommandYou can also installsimplematchfor use as a CLI command e.g. usingpipx.pipxinstallsimplematchUsageusage:simplematch[-h][--regex]pattern[strings...]positionalarguments:patternAmatchingpatternstringsThestringtomatch

options:-h,--helpshowthishelpmessageandexit--regexShowthegeneratedregularexpressionExampleExtract 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"}Backgroundsimplematchaims to fill a gap between parsing withstr.split()and regular
expressions. It should be as simple as possible, fast and stable.Thesimplematchsyntax 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!ContributionsContributions are welcome! Just submit a PR and maybe get in touch with me via email
before big changes.LicenseMIT