regex_spm
Enable Structural Pattern Matching (PEP-634) for Python regular expressions
The string goes in the place of the subject, wrapped by a regex_spm function.
The various possible regex patterns go after the cases.
Simple example
import regex_spm
match regex_spm.fullmatch_in("abracadabra"):
case r"\d+": print("It's all digits")
case r"\D+": print("There are no digits in the search string")
case _: print("It's something else")
regex_spm provides 3 functions, which correspond to re search functions:
re.search→regex_spm.search_inre.match→regex_spm.match_inre.fullmatch→regex_spm.fullmatch_in
Accessing the match object
To access the matched groups or the entire re.Match object, simply capture the pattern:
match regex_spm.fullmatch_in("123,45"):
case r"(\d+),(?P<second>\d+)" as m:
print("Notice the `as m` at the end of the line above")
print(f"The first group is {m[1]}")
print(f"The second group is {m['second']}")
print(f"The full `re.Match` object is available as {m.match}")
Pattern caveats (workaround below)
#1: Due to the mechanics of Python SPM, it is not possible to use case blocks like case re.compile(r"my-regex"): .... Python would try to use re.compile as a class name, which does not
exist.
#2: It is also not possible to save patterns to simple local variables like:
pattern1 = r"my-first-pattern"
pattern2 = re.compile(r"my-second-pattern")
match regex_spm.search_in(my_string):
case pattern1: print("This does not work, it matches any string. Python interprets `pattern1` "
"as simply a new capture variable name, hiding its previous value.")
case pattern2: print("This does not work either")
Using patterns from variables (caveat workaround)
Python requires "value patterns" to use a "dotted name", i.e. there must be a . in tha name used
to reach the stored pattern.
Putting stored patterns in a class like below is sufficient.
class PageRegexes:
index = re.compile(r"example\.com/index.html")
shopping_cart = r"example\.com/shopping_cart.html"
match regex_spm.search_in(my_url):
case PageRegexes.index: print("It's the index page")
case PageRegexes.shopping_cart: print("It's the shopping cart page")
case r"example\.com/about.html": print("You can even mix and match")
Install
Requires Python 3.10+ of course
pip install regex_spm
Metadata
Release files for regex-spm 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| regex_spm-1.0.0.tar.gz | 3.6 kB | Without provenance | Details for regex_spm-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| regex_spm-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for regex_spm-1.0.0-py3-none-any.whl |
Total release size: 7.5 kB
Release files / regex_spm-1.0.0.tar.gz
| Download URL | regex_spm-1.0.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e075e5fc04a6f22ebfa2419682d36c1662bba59cb8a1caf2339e74326f98bcef
|
|
BLAKE2b-256 checksum How to use checksums |
c19e99f27ec3376f36268dc13420b467930b774145d47efd76774053972d7a51
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Release files / regex_spm-1.0.0-py3-none-any.whl
| Download URL | regex_spm-1.0.0-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7a67b73518f49ea361c92a69a447cfbd59e171516726c959ad4c9045613bf902
|
|
BLAKE2b-256 checksum How to use checksums |
fadf190f22a7c51b4cce92127513ffead39e925b01e4d8f589794c37e3786c72
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.4
|
Security for regex-spm 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.