Get named groups from a regular expression

jkn jkn_gg at nicorp.f9.co.uk
Tue Jul 1 16:49:43 EDT 2014


On Tuesday, 1 July 2014 16:12:34 UTC+1, Florian Lindner  wrote:
> Hello,
> 
> 
> 
> Is there a way I can extract the named groups from a regular expression? 
> 
> e.g. given "(?P<testgrp>\d)" I want to get something like ["testgrp"].
> 
> 
> 
> OR
> 
> 
> 
> Can I make the match object to return default values for named groups, even 
> 
> if no match was produced?
> 
> 
> 
> Thanks,
> 
> Florian

If you can, my approach would to have a class which you use both to create
the regex (using group names), and return the names of the groups in the
regex you have created.

As I say, this might not be possible in your case though.

    Jon N



More information about the Python-list mailing list