[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

re question

wildwest

2/7/2008 6:39:00 PM

Python'ites

I searched around "google" to find the answer to this question, but I
can't:

I have a named regexp : x = re.compile("(?P<me>[a-z]+)")

What I want is an iterator, that can return me both the "groupname"
and the matched string, e.g:

m = x.search("aa")

Somehow, I want to get
{"me" : "aa"}, either as dictionary or some iterable form.


All I found is, I need to know the "groupname" to get the
corresponding match. Any help is appreciated.

A
2 Answers

wildwest

2/7/2008 6:48:00 PM

0

On Feb 7, 10:38 am, Amit Gupta <emaila...@gmail.com> wrote:
> Python'ites
>
> I searched around "google" to find the answer to this question, but I
> can't:
>
> I have a named regexp : x = re.compile("(?P<me>[a-z]+)")
>
> What I want is an iterator, that can return me both the "groupname"
> and the matched string, e.g:
>
> m = x.search("aa")
>
> Somehow, I want to get
> {"me" : "aa"}, either as dictionary or some iterable form.
>
> All I found is, I need to know the "groupname" to get the
> corresponding match. Any help is appreciated.
>
> A

Got It. re.search() has a function groupdict(), doing precisely that.

Hyuga

2/8/2008 2:34:00 PM

0

On Feb 7, 1:47 pm, Amit Gupta <emaila...@gmail.com> wrote:
> On Feb 7, 10:38 am, Amit Gupta <emaila...@gmail.com> wrote:
>
>
>
> > Python'ites
>
> > I searched around "google" to find the answer to this question, but I
> > can't:
>
> > I have a named regexp : x = re.compile("(?P<me>[a-z]+)")
>
> > What I want is an iterator, that can return me both the "groupname"
> > and the matched string, e.g:
>
> > m = x.search("aa")
>
> > Somehow, I want to get
> > {"me" : "aa"}, either as dictionary or some iterable form.
>
> > All I found is, I need to know the "groupname" to get the
> > corresponding match. Any help is appreciated.
>
> > A
>
> Got It. re.search() has a function groupdict(), doing precisely that.

Just to be pedantic so that no one else who might read this does not
get confused, re.search() does not "[have] a function groupdict()".
re.search(), like many function in the re module returns a match
object. Match objects have a groupdict() method.

Hyuga