parsing combination strings

kyosohma at gmail.com kyosohma at gmail.com
Wed Mar 21 15:10:56 EDT 2007


On Mar 21, 1:42 pm, "PKKR" <superp... at gmail.com> 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,
>
> so if i have s = "12ABA" , parser(s) should give me "12" or "1" or
> "123".
>
> I can think of a quick dirty way by checking each element in the
> string and do a 'str.isdigit()' and stop once its not a digit, but
> appreciate any eligent way.

Somehow you'll need to test each element in the string to know if it
is an integer or not. You could do the str.isdigit() method or you
could do something like this:

temp = ''
for i in yourString:
	try:
		int(i)
		temp += i
	except:
                pass


Maybe someone knows of a parser. However, that will likely be more
complex.

Mike




More information about the Python-list mailing list