[Tutor] removing whole numbers from text

Ricardo Aráoz ricaraoz at gmail.com
Mon Aug 4 16:27:29 CEST 2008


Dinesh B Vadhia wrote:
> I want to remove whole numbers from text but retain numbers attached to 
> words.  All whole numbers to be removed have a leading and trailing space.
>  
> For example, in "the cow jumped-20 feet high30er than the lazy 20 timing 
> fox who couldn't keep up the 865 meter race." remove the whole numbers 
> 20 and 865 but keep the 20 in jumped-20 and the 30 in high30er.
>  
> What is the best to do this using re?
>  
> Dinesh

 >>> text = "the cow jumped-20 feet high30er than the lazy 20 timing fox 
who couldn't keep up the 865 meter race."

 >>> ' '.join(i for i in text.split() if not i.isdigit())
"the cow jumped-20 feet high30er than the lazy timing fox who couldn't 
keep up the meter race."


HTH



More information about the Tutor mailing list