Parsing strings and obeying quotes

M

Mia

Hi All,

I'm trying to parse a string into an array of words or phrases,
something like:

my $string = "abc def ghi";
my @words = split(/ /,$string);

....but I also want to handle the case where there are quoted phrases
within the string, i.e.:

my $string = 'abc "def ghi"';
or
my $string = "abc 'def ghi'";

I want to preserve the quoted phrase as one element. Such a simple
thing, one would think, but I've not found a technique yet.

Anyone have a hint for me?

-Mia
 
A

A. Sinan Unur

Mia said:
I'm trying to parse a string into an array of words or phrases,
something like:

my $string = "abc def ghi";
my @words = split(/ /,$string);

...but I also want to handle the case where there are quoted phrases
within the string, i.e.:

my $string = 'abc "def ghi"';
or
my $string = "abc 'def ghi'";

I want to preserve the quoted phrase as one element. Such a simple
thing, one would think, but I've not found a technique yet.

Anyone have a hint for me?

You should read the Perl FAQ before posting your question:

perldoc -q inside

Sinan
 
J

J. Gleixner

Mia said:
Hi All,

I'm trying to parse a string into an array of words or phrases,
something like:

my $string = "abc def ghi";
my @words = split(/ /,$string);

...but I also want to handle the case where there are quoted phrases
within the string, i.e.:

my $string = 'abc "def ghi"';
or
my $string = "abc 'def ghi'";

I want to preserve the quoted phrase as one element. Such a simple
thing, one would think, but I've not found a technique yet.

Anyone have a hint for me?

perldoc -q delimited
 
D

Dr.Ruud

Mia schreef:
I'm trying to parse a string into an array of words or phrases,
something like:

my $string = "abc def ghi";
my @words = split(/ /,$string);

...but I also want to handle the case where there are quoted phrases
within the string, i.e.:

my $string = 'abc "def ghi"';
or
my $string = "abc 'def ghi'";

I want to preserve the quoted phrase as one element. Such a simple
thing, one would think, but I've not found a technique yet.

Anyone have a hint for me?

Think up complexer strings.

"Dungit," he said, "I left my "o'lang syne" at the baker's."
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top