Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73797 > unrolled thread

Re: Get named groups from a regular expression

Started byMRAB <python@mrabarnett.plus.com>
First post2014-07-01 17:53 +0100
Last post2014-07-01 17:53 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Get named groups from a regular expression MRAB <python@mrabarnett.plus.com> - 2014-07-01 17:53 +0100

#73797 — Re: Get named groups from a regular expression

FromMRAB <python@mrabarnett.plus.com>
Date2014-07-01 17:53 +0100
SubjectRe: Get named groups from a regular expression
Message-ID<mailman.11377.1404233586.18130.python-list@python.org>
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<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?
>
>>> import re
>>> r = re.compile(r"(?P<testgrp>\d)")
>>> r.groupindex
{'testgrp': 1}

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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web