bfish has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to use Text::Balanced to parse a string that uses single double quotes (e.g. ") to delimit and escapes a single double quote with double double quotes (e.g. ""). Here is the script I've written:

use Text::Balanced qw( extract_delimited ); use strict; my $line_in = q{""this"" "line" "has" "four"}; my $first_one; my $remainder; my $skipped; ($first_one, $remainder, $skipped) = extract_delimited($line_in, q{"}, '\s*', q{"}); print $first_one."\n"; print $remainder."\n";

And here is the output:

""
this"" "line" "has" "four"

The output I was hoping to get is:

"this"
"line" "has" "four"

What am I doing wrong?

Replies are listed 'Best First'.
Re: Using Text::Balanced
by LTjake (Prior) on Aug 20, 2003 at 18:49 UTC

    Although i've never used Text::Balance before, i think the problem is that your escaped double quote is not surrounded in double quotes itself.

    For instance, let's change the escape character to \, but leave the delimiter as is. Your string has to be "\"this\"" "line" "has" "four" to work. So, if we change the escaped char back to a double quote, it should be: """this""" "line" "has" "four" to be escaped properly.

    I did a test run and got:

    """this""" "line" "has" "four"

    as an answer.

    I may be way off, but i tried. :)

    HTH

    --
    "To err is human, but to really foul things up you need a computer." --Paul Ehrlich

      Oh, it is so bad when I can't even manage the simple act of setting up a test case. Thanks HTH for clearing up the obvious.
Re: Using Text::Balanced
by bear0053 (Hermit) on Aug 20, 2003 at 18:40 UTC
    You program is doing what you tell it to. Since you input is:
    {""this"" "line" "has" "four"}
    When it delimits at " the first thing it retrieves is "" then 'this' then the last pair of qoute. Then the remaining elements are fine. Try to format your input like this:
    {"this" "line" "has" "four"}
    To make it work for double " try a find and replace on "" with "
    =~ s/""/"/g;