Extracting search criteria using re

Robert W. Bill rbill at digisprings.com
Mon Jul 31 14:54:44 EDT 2000


On Mon, 31 Jul 2000 pauljolly at my-deja.com wrote:
> Dear All,
> 
> I am using the re module to search for a particular string in an html
> file (not within the tags but within the page material). This question
> is more about regular expression technique more that anything.
> 
> I have read the manual on regular expression but cannot seem to find
> 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.
> 
> Regards,
> 
> Paul Jolly


Maybe findall() will help?
i.e.-
>>>import re
>>>s = "This is an example of two temperatures 56 ° F and 36 ° F."
>>>re.compile("\d?\d\s* °")
>>>re.findall(s)
['56 °', '36 °']

-robert




More information about the Python-list mailing list