By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

Working with named groups in re module

P: n/a
A found some clues on lexing using the re module in Python in an
article by Martin L÷wis.

http://www.python.org/community/sigs...ards-standard/

He writes:
[...]
A scanner based on regular expressions is usually implemented
as an alternative of all token definitions. For XPath, a
fragment of this expressions looks like this:
(?P<Number>\\d+(\\.\\d*)?|\\.\\d+)|
(?P<VariableReference>\\$""" + QName + """)|
(?P<NCName>"""+NCName+""")|
(?P<QName>"""+QName+""")|
(?P<LPAREN>\\()|

Here, each alternative in the regular expression defines a
named group. Scanning proceeds in the following steps:

1. Given the complete input, match the regular expression
with the beginning of the input.
2. Find out which alternative matched.
[...]

Item 2 is where I get stuck. There doesn't seem to be an obvious
way to do it, which I understand is a bad thing in Python.
Whatever source code went with the article originally is not
linked from the above page, so I don't know what Martin did.

Here's what I came up with (with a trivial example regex):

import re
r = re.compile('(?P<x>x+)|(?P<a>a+)')
m = r.match('aaxaxx')
if m:
for k in r.groupindex:
if m.group(k):
# Find the token type.
token = (k, m.group())

I wish I could do something obvious instead, like m.name().

--
Neil Cerutti
After finding no qualified candidates for the position of principal, the
school board is pleased to announce the appointment of David Steele to the
post. --Philip Streifer
Jan 10 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Neil Cerutti wrote:
A found some clues on lexing using the re module in Python in an
article by Martin L÷wis.
Here, each alternative in the regular expression defines a
named group. Scanning proceeds in the following steps:

1. Given the complete input, match the regular expression
with the beginning of the input.
2. Find out which alternative matched.
you can use lastgroup, or lastindex:

http://effbot.org/zone/xml-scanner.htm

there's also a "hidden" ready-made scanner class inside the SRE module
that works pretty well for simple cases; see:

http://aspn.activestate.com/ASPN/Coo.../Recipe/457664

</F>

Jan 10 '07 #2

P: n/a
On 2007-01-10, Fredrik Lundh <fr*****@pythonware.comwrote:
Neil Cerutti wrote:
>A found some clues on lexing using the re module in Python in
an article by Martin L÷wis.
> Here, each alternative in the regular expression defines a
named group. Scanning proceeds in the following steps:

1. Given the complete input, match the regular expression
with the beginning of the input.
2. Find out which alternative matched.

you can use lastgroup, or lastindex:

http://effbot.org/zone/xml-scanner.htm

there's also a "hidden" ready-made scanner class inside the SRE
module that works pretty well for simple cases; see:

http://aspn.activestate.com/ASPN/Coo.../Recipe/457664
Thanks for the excellent pointers.

I got tripped up:
>>m = re.match('(a+(b*)a+)', 'abbbbaa')
dir(m)
['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start']

There are some notable omissions there. That's not much of an
excuse for my not understanding the handy docs, but I guess it
can can function as a warning against relying on the interactive
help.

I'd seen the lastgroup definition in the documentation, but I
realize it was exactly what I needed. I didn't think carefully
enough about what "last matched capturing group" actually meant,
given my regex. I don't think I saw "name" there either. ;-)

lastgroup

The name of the last matched capturing group, or None if the
group didn't have a name, or if no group was matched at all.

--
Neil Cerutti
We dispense with accuracy --sign at New York drug store
Jan 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.