re question

A

Amit Gupta

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
 
A

Amit Gupta

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.
 
H

Hyuga

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

re Questions 9
Re for Apache log file format 4
Register Question 0
subprocess question re waiting 4
Contact form question 2
Question regarding re module 1
RE Help splitting CVS data 7
re question 1

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top