Simple question: combine a quoted string into a single token

S

Squeamizh

Hi,

I have a program which separates each line of a text file into tokens,
using whitespace as a delimiter (I do this with String.split). This
suits my needs for the most part, but now I need the ability to treat
quoted strings as single tokens. Note that the quoted string could be
multiple words, or even a 0-length string.

Could anyone recommend a basic strategy for doing this? Should I deal
with this when I first tokenize each line, or should I combine tokens
appropriately during parsing when I see a double-quote?

Help would be greatly appreciated.
 
K

Kevin Olbrich

Hi,

I have a program which separates each line of a text file into tokens,
using whitespace as a delimiter (I do this with String.split). This
suits my needs for the most part, but now I need the ability to treat
quoted strings as single tokens. Note that the quoted string could be
multiple words, or even a 0-length string.

Could anyone recommend a basic strategy for doing this? Should I deal
with this when I first tokenize each line, or should I combine tokens
appropriately during parsing when I see a double-quote?

Help would be greatly appreciated.

You could pull out all the quoted strings into an array and then delete
them from the original before processing it normally.


_Kevin
www.sciwerks.com
 
R

Robert Klemme

Squeamizh said:
Hi,

I have a program which separates each line of a text file into tokens,
using whitespace as a delimiter (I do this with String.split). This
suits my needs for the most part, but now I need the ability to treat
quoted strings as single tokens. Note that the quoted string could be
multiple words, or even a 0-length string.

Could anyone recommend a basic strategy for doing this? Should I deal
with this when I first tokenize each line, or should I combine tokens
appropriately during parsing when I see a double-quote?

Help would be greatly appreciated.

Something along the lines of

line.scan %r{
"[^"]*" |
\S+
}x

HTH

robert
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top