Skip to content

A mini-library for destructive group matching of regexes in Python using recently proposed Structural Pattern Matching (https://www.python.org/dev/peps/pep-0622/).

License

Notifications You must be signed in to change notification settings

femoiseev/patma-regex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

patma-regex

A mini-library for Scala-like destructive group matching of regexes in Python using recently proposed Structural Pattern Matching.

Installation

First of all, you need to install fork of alpha version of Python 3.10 that implements match / case clause ( https://github.com/brandtbucher/cpython/tree/patma):

git clone --single-branch --branch patma https://github.com/brandtbucher/cpython.git
cd cpython
./configure
make
make test
sudo make altinstall

The command above will install alpha-version of python 3.10 with implemented pattern matching (don't worry, it won't override your system python, just add python3.10 into list of your python versions).

After that, you can just install this library from PyPi (I suggest you to do in into a virtualenv environment):

python3.10 -m venv venv
source venv/bin/activate
pip install patma-regex

Usage

This library allows you to use destructive matching of groups in regexes in Scala-like way. Here I provide few examples (to run them, use virtualenv environment described in the previous section):

You can extract positional groups:

from patma_regex import PatmaRegex

pattern = PatmaRegex("(\\w+) (\\w+)")

# The following code will print "John | Doe"
match "John Doe":
    case pattern(x, y):
        print(x + " | " + y)

Also, you can extract named groups:

from patma_regex import PatmaRegex

pattern = PatmaRegex("(?P<firstname>\\w+) (?P<lastname>\\w+)")

# The following code will print "John | Doe"
match "John Doe":
    case pattern(firstname=x, lastname=y):
        print(x + " | " + y)

It's not necessary to refer to named groups by name, you can also do it in a positional way:

from patma_regex import PatmaRegex

pattern = PatmaRegex("(?P<firstname>\\w+) (?P<lastname>\\w+)")

# The following code will print "John | Doe"
match "John Doe":
    case pattern(x, lastname=y):
        print(x + " | " + y)

By default, it's checked whether full string matches regular expression. If you want to check if some substring matches regular expression, use unanchored=True:

from patma_regex import PatmaRegex

pattern = PatmaRegex("(\\w+) (\\w+)", unanchored=True)

# The following code will print "John | Doe"
match "   John Doe   ;  ;":
    case pattern(x, y):
        print(x + " | " + y)

Of course, you can put few case into match statement, read PEP for more details.

About

A mini-library for destructive group matching of regexes in Python using recently proposed Structural Pattern Matching (https://www.python.org/dev/peps/pep-0622/).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages