Iterate over group names in a regex match?

B

Brian D

Here's a simple named group matching pattern:
('1', '2', '3')

Is it possible to call the group names, so that I can iterate over
them?

The result I'm looking for would be:

('one', 'two', 'three')
 
P

Peter Otten

Brian said:
Here's a simple named group matching pattern:

('1', '2', '3')

Is it possible to call the group names, so that I can iterate over
them?

The result I'm looking for would be:

('one', 'two', 'three')
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
'groups', 'span', 'start']
m.groupdict().keys() ['one', 'three', 'two']
sorted(m.groupdict(), key=m.span)
['one', 'two', 'three']

Peter
 
A

Alf P. Steinbach

* Brian D:
Here's a simple named group matching pattern:

('1', '2', '3')

Is it possible to call the group names, so that I can iterate over
them?

The result I'm looking for would be:

('one', 'two', 'three')

I never used that beast (I'm in a sense pretty new to Python, although starting
some months back I only investigate what's needed for my writings), but checking
things in the interpreter:

['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups',
'span', 'start']

Cheers & hth.,

- Alf
 
B

Brian D

Brian said:
Here's a simple named group matching pattern:

('1', '2', '3')
Is it possible to call the group names, so that I can iterate over
them?
The result I'm looking for would be:
('one', 'two', 'three')

['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
'groups', 'span', 'start']>>> m.groupdict().keys()

['one', 'three', 'two']>>> sorted(m.groupdict(), key=m.span)

['one', 'two', 'three']

Peter

groupdict() does it. I've never seen it used before. Very cool!

Thank you all for taking time to answer the question.
 
B

Brian D

['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict',
'groups', 'span', 'start']>>> m.groupdict().keys()
['one', 'three', 'two']>>> sorted(m.groupdict(), key=m.span)
['one', 'two', 'three']

groupdict() does it. I've never seen it used before. Very cool!

Thank you all for taking time to answer the question.


FYI, here's an example of the working result ...
k, v


('one', '1')
('three', '3')
('two', '2')


The use for this is that I'm pulling data from a flat text file using
regex, and storing values in a dictionary that will be used to update
a database.
 
M

MRAB

Brian said:
Here's a simple named group matching pattern:

('1', '2', '3')

Is it possible to call the group names, so that I can iterate over
them?

The result I'm looking for would be:

('one', 'two', 'three')
The closest you can get is with groupdict():
{'one': '1', 'three': '3', 'two': '2'}
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top