Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #73792 > unrolled thread
| Started by | Florian Lindner <mailinglists@xgm.de> |
|---|---|
| First post | 2014-07-01 17:12 +0200 |
| Last post | 2014-07-03 23:30 +0000 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Get named groups from a regular expression Florian Lindner <mailinglists@xgm.de> - 2014-07-01 17:12 +0200
Re: Get named groups from a regular expression jkn <jkn_gg@nicorp.f9.co.uk> - 2014-07-01 13:49 -0700
Re: Get named groups from a regular expression Philip Shaw <jnufcvyvuc@tznvy.pbz> - 2014-07-03 23:30 +0000
| From | Florian Lindner <mailinglists@xgm.de> |
|---|---|
| Date | 2014-07-01 17:12 +0200 |
| Subject | Get named groups from a regular expression |
| Message-ID | <mailman.11370.1404227582.18130.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | jkn <jkn_gg@nicorp.f9.co.uk> |
|---|---|
| Date | 2014-07-01 13:49 -0700 |
| Message-ID | <f6d1bf0e-1b70-491e-b8a1-3541c039307f@googlegroups.com> |
| In reply to | #73792 |
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
[toc] | [prev] | [next] | [standalone]
| From | Philip Shaw <jnufcvyvuc@tznvy.pbz> |
|---|---|
| Date | 2014-07-03 23:30 +0000 |
| Message-ID | <53b5e78f$0$2911$c3e8da3$76491128@news.astraweb.com> |
| In reply to | #73792 |
On 2014-07-01, Florian Lindner <mailinglists@xgm.de> wrote: > > 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"]. 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web