Using re - side effects or misunderstanding
Sune Kirkeby
sune at interspace.dk
Sun Jan 14 06:06:05 EST 2001
[ "Andrew Henshaw" <andrew_dot_henshaw_at_earthling_dot_net> ]
> But I thought that ?: is for matching but not returning. And so it is,
> under certain circumstances, e.g.
It always is, and it always does.
> if my pattern is
> '(ab)(c)xyz'
> I get
> [('ab', 'c')] (Yikes! a tuple. I'm going to have to change my code a
> bit to handle this)
> but
> '(ab)(?:c)xyz'
> yields,
> ['ab']
> and
> '(?:ab)(?:c)xyz'
> gives
> ['abcxyz']
All of the above are what one would expect, since re.findall returns
a list of matches, if there are no groups in the re. But if there are
groups it will _only_ return a list of tuples with the matched groups.
Note that (?:...) is non-grouping, so in '(?:ab)(?:c)xyz' there are
no groups, but in '(ab)(?:c)xyz' there is one group, which will then
be returned.
> so how do I get the result
> ['abxyz']
> ??
Something along the lines of,
>>> r = re.compile('(ab)c(xyz)')
>>> matches = r.findall('..abcxyz..')
>>> matches
[('ab', 'xyz')]
almost there, just have to join the tuples,
>>> map(lambda l: string.join(l, ''), matches)
['abxyz']
Voila!
> In other words, adding groups for the purpose of adding repetitions seems to
> have a greater side-effect than I would desire. Is there something that I'm
> missing in my use of re's?
Yes, you weren't using groups (all the time, anyway) :-).
first-posting-ly yrs'
--
Sune Kirkeby | "Imagine, if you will, that there were no
http://mel.interspace.dk/~sune/ | such thing as a hypothetical situation..."
More information about the Python-list
mailing list