See https://bugs.python.org/issue46692. It's not so easy to make match objects mappings or sequences because of the len() problem.

Eric

On 2/16/2022 9:46 AM, Valentin Berlier wrote:
Hi,

I've been thinking that it would be nice if regex match objects could be 
deconstructed with pattern matching. For example, a simple .obj parser could 
use it like this:

     match re.match(r"(v|f) (\d+) (\d+) (\d+)", line):
         case ["v", x, y, z]:
             print("Handle vertex")
         case ["f", a, b, c]:
             print("Handle face")

Sequence patterns would extract groups directly. Mapping patterns could be used 
to extract named groups, which would be nice for simple parsers/tokenizers:

     match re.match(r"(?P<number>\d+)|(?P<add>\+)|(?P<mul>\*)", line):
         case {"number": str(value)}:
             return Token(type="number", value=int(value))
         case {"add": str()}:
             return Token(type="add")
         case {"mul": str()}:
             return Token(type="mul")

Right now, match objects aren't proper sequence or mapping types though, but 
that doesn't seem too complicated to achieve. If this is something that enough 
people would consider useful I'm willing to look into how to implement this.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/EKMIJCSJGHJR36W2CNJE4CKO3S5MW3U4/
Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BA6TGJJN65246H7MWYLTUGFSEJ2U2KQ7/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to