473,412 Members | 2,067 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,412 software developers and data experts.

Help: How to parse out tokens in this string

I have a long string (MyString) with several token/value combinations.
Here's an example...

NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE
PHONE: 333-222-5151 x542 EMAIL: SO**@SOME.COM

How can I easily parse out a few of these tokens? For example, if I want to
get the COUNTY, or PHONE?

I would think that the split function would work, but this doesn't seem to
work. Here's an example...

MyString.split("PHONE:")(1).split("EMAIL: ")(0)

My logic: First do a split and get the phone number portion, then "cut off"
everything after the phone number by "splitting" again at the email and
taking the first token, which would contain the phone number.

But this doesn't seem to work. Am I even close?????

Thanks!
Aug 15 '08 #1
7 1385
Cirene used his keyboard to write :
I have a long string (MyString) with several token/value combinations. Here's
an example...

NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE
PHONE: 333-222-5151 x542 EMAIL: SO**@SOME.COM

How can I easily parse out a few of these tokens? For example, if I want to
get the COUNTY, or PHONE?

I would think that the split function would work, but this doesn't seem to
work. Here's an example...

MyString.split("PHONE:")(1).split("EMAIL: ")(0)

My logic: First do a split and get the phone number portion, then "cut off"
everything after the phone number by "splitting" again at the email and
taking the first token, which would contain the phone number.

But this doesn't seem to work. Am I even close?????

Thanks!
A first attempt:
1. look for the first ':'
2. walk back to the last non-space (watch out for the beginning of the
line) -this is your "key"
3. look for the next ':' (watch out for the end of the line)
4. walk back to the first space, walk back again to the first non-space
(to skip the whitespace betweek the key-value pairs) -from the ':' in
step 1 to here is your "value"
5. put key and value in a dictionary
6. repeat (you could have remembered the next key's positions from step
4)

If you know you have a single TAB between the key-value pairs, it would
be much easier: first split on the TAB to get key-value pairs, then
split them on the ':'.
Hans Kesting
Aug 15 '08 #2
"Cirene" <ci****@nowhere.comwrote in message
news:eY**************@TK2MSFTNGP04.phx.gbl...
But this doesn't seem to work. Am I even close?????
Firstly, are the various name/value pairs fixed width or separated?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 15 '08 #3
I have to find out from the client. But, it would be nice to know how to do
it either way...

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Cirene" <ci****@nowhere.comwrote in message
news:eY**************@TK2MSFTNGP04.phx.gbl...
>But this doesn't seem to work. Am I even close?????

Firstly, are the various name/value pairs fixed width or separated?
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 15 '08 #4
"Cirene" <ci****@nowhere.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]
>>But this doesn't seem to work. Am I even close?????

Firstly, are the various name/value pairs fixed width or separated?

I have to find out from the client. But, it would be nice to know how to
do it either way...

I have to find out from the client. But, it would be nice to know how to
do it either way...
If it's fixed width, use Substring()
If it's delimited, use Split()
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 15 '08 #5
Thanks Mark! What if it's just variable length token/values, such as in the
example?'

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uY****************@TK2MSFTNGP04.phx.gbl...
"Cirene" <ci****@nowhere.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...

[top-posting corrected]
>>>But this doesn't seem to work. Am I even close?????

Firstly, are the various name/value pairs fixed width or separated?

I have to find out from the client. But, it would be nice to know how to
do it either way...

I have to find out from the client. But, it would be nice to know how to
do it either way...

If it's fixed width, use Substring()
If it's delimited, use Split()
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 15 '08 #6
"Cirene" <ci****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...

[top-posting corrected again] <======
>>>>But this doesn't seem to work. Am I even close?????

Firstly, are the various name/value pairs fixed width or separated?

I have to find out from the client. But, it would be nice to know how
to do it either way...

I have to find out from the client. But, it would be nice to know how
to do it either way...

If it's fixed width, use Substring()
If it's delimited, use Split()

What if it's just variable length token/values, such as in the example?'
In that case, you'll need to follow Hans' advice...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 15 '08 #7
Thanks!

"Hans Kesting" <ne*********@spamgourmet.comwrote in message
news:ev**************@TK2MSFTNGP06.phx.gbl...
Cirene used his keyboard to write :
>I have a long string (MyString) with several token/value combinations.
Here's an example...

NAME: JOHN ADDRESS: 123 MAIN ST COUNTY: SOMEWHERE
PHONE: 333-222-5151 x542 EMAIL: SO**@SOME.COM

How can I easily parse out a few of these tokens? For example, if I want
to get the COUNTY, or PHONE?

I would think that the split function would work, but this doesn't seem
to work. Here's an example...

MyString.split("PHONE:")(1).split("EMAIL: ")(0)

My logic: First do a split and get the phone number portion, then "cut
off" everything after the phone number by "splitting" again at the email
and taking the first token, which would contain the phone number.

But this doesn't seem to work. Am I even close?????

Thanks!

A first attempt:
1. look for the first ':'
2. walk back to the last non-space (watch out for the beginning of the
line) -this is your "key"
3. look for the next ':' (watch out for the end of the line)
4. walk back to the first space, walk back again to the first non-space
(to skip the whitespace betweek the key-value pairs) -from the ':' in
step 1 to here is your "value"
5. put key and value in a dictionary
6. repeat (you could have remembered the next key's positions from step 4)

If you know you have a single TAB between the key-value pairs, it would be
much easier: first split on the TAB to get key-value pairs, then split
them on the ':'.
Hans Kesting


Aug 15 '08 #8

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

Similar topics

5
by: Mad Scientist Jr | last post by:
Has anyone worked on code that that can parse evaluation expressions (could be numbers or strings) like ( ( "dog" = "dog" ) or "foo" = "bar" ) and ("cow" = "bat" and "bye" = "hi") or ("math" =...
22
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to...
5
by: Knackeback | last post by:
task: - read/parse CSV file code snippet: string key,line; typedef tokenizer<char_separator<char> > tokenizer; tokenizer tok(string(""), sep); while ( getline(f, line) ){ ++lineNo;...
21
by: William Stacey [MVP] | last post by:
Anyone know of some library that will parse files like following: options { directory "/etc"; allow-query { any; }; // This is the default recursion no; listen-on { 192.168.0.225;...
8
by: rh0dium | last post by:
Hi all, I am using python to drive another tool using pexpect. The values which I get back I would like to automatically put into a list if there is more than one return value. They provide me...
8
by: Artemio | last post by:
Dear folks, I need some help with using the sscanf() function. I need to parse a string which has several parameters given in a "A=... B=... C=..." way, and each has a different type (one is a...
6
by: Richard | last post by:
Which way would you guys recommened to best parse a multiline file which contains two fields seperated by a tab. In this case its the linux/proc/filesystems file a sample of which I have included...
1
by: Steve | last post by:
Hi All (especially Paul McGuire!) Could you lend a hand in the grammar and paring of the output from the function win32pdhutil.ShowAllProcesses()? This is the code that I have so far (it is...
7
by: Donn Ingle | last post by:
Hi, I really hope someone can help me -- I'm stuck. I have written three versions of code over a week and still can't get past this problem, it's blocking my path to getting other code written. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.