Help: How to parse out tokens in this string

C

Cirene

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: (e-mail address removed)

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!
 
H

Hans Kesting

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: (e-mail address removed)

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
 
C

Cirene

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

Cirene

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

Mark Rae said:
[top-posting corrected]
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()
 
C

Cirene

Thanks!

Hans Kesting said:
Cirene used his keyboard to write :

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top