Working with named groups in re module

Fredrik Lundh fredrik at
Wed Jan 10 16:18:59 CET 2007

Neil Cerutti wrote:

> A found some clues on lexing using the re module in Python in an
> article by Martin L÷wis.

>   Here, each alternative in the regular expression defines a
>   named group. Scanning proceeds in the following steps:
>      1. Given the complete input, match the regular expression
>      with the beginning of the input.
>      2. Find out which alternative matched.

you can use lastgroup, or lastindex:

there's also a "hidden" ready-made scanner class inside the SRE module
that works pretty well for simple cases; see:


More information about the Python-list mailing list