[Tutor] Finding all the letters in a string?

Michael Langford mlangford.cs03 at gtalumni.org
Tue Sep 18 02:37:38 CEST 2007


Man...really not my night:

import re

def getLettersOnly( chars ) :
     pat = re.compile('[a-zA-Z]')
     return ''.join(pat.findall(chars))

if __name__ == "__main__":
    print getLettersOnly("afdlkjal32jro3kjlkj(*&&^%&^TUHKLJDHFKJHS(*&987")

Which would produce:
   afdlkjaljrokjlkjTUHKLJDHFKJHS

    --Michael

-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com

On 9/17/07, Michael Langford <mlangford.cs03 at gtalumni.org> wrote:
>
> The original question was asking for a list of the letters (or a string
> with them). Not all the alpha-numeric characters.
>
> \w would leave in the numbers, which is not what he wanted. If you wanted
> to base the solution off regex, then you'd need to go:
>
>
> import re
>
> def getLettersOnly( chars ) :
>      pat = re.compile('[a-zA-Z]')
>      return ''.join.pat.findall(chars)
>
> if __name__ == "__main__":
>     print getLettersOnly("afdlkjal32jro3kjlkj(*&&^%&^TUHKLJDHFKJHS(*&987")
>
>
>     --Michael
>
> --
> Michael Langford
> Phone: 404-386-0495
> Consulting: http://www.TierOneDesign.com/
> Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com
>
> On 9/17/07, Eric Lake <ericlake at ubuntu.com> wrote:
>
> > This seems to work to get out the alpha-numeric characters.
> >
> > #!/usr/bin/env python
> > # -*- coding: iso-8859-15 -*-
> >
> > import re
> >
> > pat = re.compile('\w')
> >
> > lst = []
> >
> > chars = '@*1&^$&*^@$g*(&@2$*(&$@c(*&3*(&c^&%4&^%'
> >
> > lst = pat.findall(chars)
> > for x in lst:
> >     print x,
> >
> >
> > --
> >
> > Thanks
> > Eric Lake
> >
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v1.4.6 (GNU/Linux)
> >
> > iQEVAwUBRu8ZvpLZLpR+JU3MAQrcXgf9HALZxw0jcZeI1I90GO2gpj3EonMUzaag
> > bNPyY6NNjai8QEv/UpnhWBsNNibZ5GsmTH419YspJvncz95eqqukjDWU2bfBX2Zv
> > WZ/Mgz14W2Kjx2mYExsujGmOIahc7JNKsm8w3gtoBWrUKOBxcwOW88wgRHm/cbka
> > zVehXczP4tDyOf9rjQ04nxHVxIxV5J5IisLyXwIOZXbsOtdOAsMbuow41VJy8JFZ
> > 593+Ngav2a/RFjglkohwJTw9nj4sUzcU8VkDAuzdf5eoOXdAKuCLdk0dyp2CJ09w
> > o4dXZ/qQb/cgvctsshad2+78f7bZyTbyub2qxM79NHiCgBZeh1ZdHg==
> > =oa8G
> > -----END PGP SIGNATURE-----
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070917/b3cadb8b/attachment.htm 


More information about the Tutor mailing list