Using re - side effects or misunderstanding

Alex Martelli aleaxit at yahoo.com
Sat Jan 13 18:49:50 EST 2001


"Andrew Henshaw" <andrew_dot_henshaw_at_earthling_dot_net> wrote in message
news:t61f9nsrgdcj0f at corp.supernews.com...
    [snip]
> and I execute the following
>
>     r.findall('..abcxyz..')
    [snip]
> so how do I get the result
>
>     ['abxyz']

findall will never return a string that is not a substring of the string you
pass
it as an argument.  As abxyz is not a substring of '..abcxyz..', it will
_never_
be returned by findall, whatever r is.

> 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?

Rather, it seems to me, in findall (admittedly a somewhat peculiar method).

If you want, in your example, to (e.g.) match 1 or more 'abc's, then 0 or
more 'c's to be ignored, then 1 or more 'xyz's, what you'll get from findall
(with first and third normal group, second non-matching group) is a [list
containing a] tuple ('abc','xyz').  You need to ''.join it to make it into a
string; findall won't do that for you.


Alex






More information about the Python-list mailing list