Extracting search criteria using re

Alex Martelli alex at magenta.com
Mon Jul 31 07:09:23 EDT 2000


<pauljolly at my-deja.com> wrote in message news:8m3i61$hsm$1 at nnrp1.deja.com...
> Sorry, I have found the answer to my question. I now use the findall()
> function to extract the data. Sorry to trouble you all (but my other
> question is still valid!!)

What other question...?

> > anything that answers my question: when using the
> > (recompiledobject).split() method, I want to return a list of all the
> > occurences of my search criteria and not what remains when my search
> > criteria is stripped from the string. For example, searching using the
> > following search string: \d?\d\s*'°' in the following:
> >
> > "This is an example of two temperatures 56 ° F and 36 ° F."
> >
> > Applying the split function on the above, I would want to return as a
> > list ('56 °', '36 °'). I am sure this is possible but cannot
> > for the life of me find the answer. Any help much appreciated.

The split method just doesn't do that.  It never suppresses data outside
of the matches.  It can give you either just that data-outside, or that
plus the capturing-groups in the matches if you have such groups in
your RE (i.e., parentheses in it).  findall, as you noticed, is the method
that suppresses the data outside of the matches.  Why would you want
to use a method that doesn't do what you want, when there's one that
does?


Alex






More information about the Python-list mailing list