Re: parsing combination strings

2007-03-21 Thread bearophileHUGS
Bruno Desthuilliers:
> exp = re.compile(r'^([0-9]+)')
> for s in ["12ABA", "1ACD", "123CSD"]:
>  print exp.match(line).group(0)

This may be enough too:

exp = re.compile(r'\d+')
for s in ["12ABA", "1ACD", "123CSD"]:
print exp.match(line).group(0)

With it:
exp.match("a123CSD") = None

Bye,
bearophile

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread Bruno Desthuilliers
PKKR a écrit :
> On Mar 21, 2:51 pm, "Steven D. Arnold" <[EMAIL PROTECTED]> wrote:
> 
>>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
> 
> 
> 
> yep thats what i tried as per tommy's advice and came up with:
> 
> re.split('[^0-9]', str)[0]

avoid using 'str' as an identifier (unless it's ok for you to shadow the 
builtin string type)

> or is there a better way?
> 
exp = re.compile(r'^([0-9]+)')
for s in ["12ABA", "1ACD", "123CSD"]:
 print exp.match(line).group(0)
=> 12
=> 1
=> 123
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread PKKR
On Mar 21, 2:51 pm, "Steven D. Arnold" <[EMAIL PROTECTED]> wrote:
> 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


yep thats what i tried as per tommy's advice and came up with:

re.split('[^0-9]', str)[0]

or is there a better way?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread PKKR
ah ok, i guess something like this should do it for me then?

re.split('[^0-9]', str)[0]


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread kyosohma
On Mar 21, 1:42 pm, "PKKR" <[EMAIL PROTECTED]> 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread Steven D. Arnold
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: parsing combination strings

2007-03-21 Thread Tommy Nordgren

On 21 mar 2007, at 19.42, 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,
>
> 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.
You need to look up regular expressions in the Documentation.
A regular expression that matches only at the start of the string is  
appropriate
for this problem.

--
"Home is not where you are born, but where your heart finds peace" -
Tommy Nordgren, "The dying old crone"
[EMAIL PROTECTED]


-- 
http://mail.python.org/mailman/listinfo/python-list


parsing combination strings

2007-03-21 Thread PKKR
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.

-- 
http://mail.python.org/mailman/listinfo/python-list