parsing combination strings

Steven D. Arnold stevena at neosynapse.net
Wed Mar 21 14:51:58 EDT 2007


On Mar 21, 2007, at 2:42 PM, PKKR wrote:
> I need a fast and efficient way to parse a combination string(digits +
> chars)
>
> ex: s = "12ABA" or "1ACD" or "123CSD" etc
>
> I want to parse the the above string such that i can grab only the
> first digits and ignore the rest of the chacters,

A regex leaps to mind.....have you investigated the "re" module?

 >>> import re
 >>> re.match(r'(\d+)', '123abc').group(1)
'123'

steven




More information about the Python-list mailing list