Re: Get named groups from a regular expression

2014-07-03 Thread Philip Shaw
On 2014-07-01, Florian Lindner  wrote:
>
> Is there a way I can extract the named groups from a regular
> expression?  e.g. given "(?P\d)" I want to get something
> like ["testgrp"].

The match object has an attribute called "groupdict", so you can get
the found named groups using match.groupdict.keys. I can't remember
what happens to unnamed groups (I prefer to name every group I want),
but ISTR that there is a list of capture groups in which the indexes
are the capture groups number (i.e. what you'd use to backreference
them).

> Can I make the match object to return default values for named
> groups, even if no match was produced?

A lazy solution I've used was to write a default dict, then update it
with the groupdict. I doubt that's all that efficient, but the
defaults were constant strings and the program was network-bound
anyway.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-02 Thread Devin Jeanpierre
On Tue, Jul 1, 2014 at 8:58 AM, Chris Angelico  wrote:
> On Wed, Jul 2, 2014 at 1:29 AM, Peter Otten <__pete...@web.de> wrote:
>> Easy, just write a regular expression to parse regular expressions ;)
>
> Hmm, is that even possible? AIUI you can't make a regex that correctly
> parses nested tokens, and named groups can definitely be nested.

Nesting isn't inherently a problem. Since nesting doesn't change the
way they parse, you can ignore nesting for the purposes of pulling out
named groups. Find each unescaped "(?P<...>".

(Making sure they are unescaped is annoying, though.)

-- Devin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-01 Thread jkn
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\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
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-01 Thread Peter Otten
Chris Angelico wrote:

> On Wed, Jul 2, 2014 at 1:29 AM, Peter Otten <__pete...@web.de> wrote:
>> Easy, just write a regular expression to parse regular expressions ;)
> 
> Hmm, is that even possible? AIUI you can't make a regex that correctly
> parses nested tokens, and named groups can definitely be nested.

Hmm, it was a joke.

For a limited number of nestings you should be able to cook something up; I 
think Python allows up to 100 groups, so there is an upper limit of nesting 
levels. You might need a regex implementation that allows more than 100 
groups to parse your regex-parsing regex though...

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-01 Thread MRAB

On 2014-07-01 16:12, Florian Lindner wrote:

Hello,

Is there a way I can extract the named groups from a regular expression?
e.g. given "(?P\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?


import re
r = re.compile(r"(?P\d)")
r.groupindex

{'testgrp': 1}

i.e. there's a named group called 'testgrp' whose group number is 1.

--
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-01 Thread Chris Angelico
On Wed, Jul 2, 2014 at 1:29 AM, Peter Otten <__pete...@web.de> wrote:
> Easy, just write a regular expression to parse regular expressions ;)

Hmm, is that even possible? AIUI you can't make a regex that correctly
parses nested tokens, and named groups can definitely be nested.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Get named groups from a regular expression

2014-07-01 Thread Peter Otten
Florian Lindner wrote:

> Is there a way I can extract the named groups from a regular expression?
> e.g. given "(?P\d)" I want to get something like ["testgrp"].

Easy, just write a regular expression to parse regular expressions ;)

(Sorry, I can't contribute something constructive, my first idea, re.DEBUG 
doesn't help)

> OR
> 
> Can I make the match object to return default values for named groups,
> even if no match was produced?


-- 
https://mail.python.org/mailman/listinfo/python-list