473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

parsing combination strings

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.

Mar 21 '07 #1
7 1193

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"
to************@comhem.se
Mar 21 '07 #2
On Mar 21, 1:42 pm, "PKKR" <superp...@gmail.comwrote:
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

Mar 21 '07 #3
ah ok, i guess something like this should do it for me then?

re.split('[^0-9]', str)[0]
Mar 21 '07 #4
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

Mar 21 '07 #5
On Mar 21, 2:51 pm, "Steven D. Arnold" <stev...@neosynapse.netwrote:
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?

Mar 21 '07 #6
PKKR a écrit :
On Mar 21, 2:51 pm, "Steven D. Arnold" <stev...@neosynapse.netwrote:
>>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
Mar 21 '07 #7
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

Mar 21 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
2
by: Todd Moyer | last post by:
I would like to use Python to parse a *python-like* data description language. That is, it would have it's own keywords, but would have a syntax like Python. For instance: Ob1 ('A'): Ob2...
5
by: Aleksandar Matijaca | last post by:
Hi there, I am in some need of help. I am trying to parse using the apache sax parser a file that has vaid UTF-8 characters - I keep end up getting a sun.io.MalformedInputException error. ...
10
by: Christopher Benson-Manica | last post by:
(if this is a FAQ, I apologize for not finding it) I have a C-style string that I'd like to cleanly separate into tokens (based on the '.' character) and then convert those tokens to unsigned...
4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
6
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
7
by: Lucas Tam | last post by:
Hi all, Does anyone know of a GOOD example on parsing text with text qualifiers? I am hoping to parse text with variable length delimiters/qualifiers. Also, qualified text could run onto...
9
by: paragpdoke | last post by:
Hello All. I'm looking for some algorithm to build a combination of strings from multiple arrays. Let me explain in detail. - I'm working on VBA (excel). I have functions that accept one string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.