in reply to split on unescaped delimiters

Here is a use of a negative lookbehind zero-width assertion to prevent a split on a comma if it is preceeded by a backslash.

my @array = split /(?<!\\),/, $string;

It looks like your code is using a colon as the delimiter. This solution can be easily adapted to whatever delimiter or escape sequence you desire.

For more elaborate things, like balanced quotes, you're better off going to a Text::Balanced module, or maybe the DBD::CSV module.

Update: To answer the escaped escape problem that you've mentioned, you could resort to alternation within the split:

my @array = split/(?:\\\\,)|(?:(?<!\\),)/, $string;

You really end up with some ugly leaning toothpicks!


Dave

Replies are listed 'Best First'.
Re: Re: split on unescaped delimiters
by bsb (Priest) on Jan 08, 2004 at 09:43 UTC
    But the backslash might be backslashed
Re: Re: split on unescaped delimiters
by bsb (Priest) on Jan 08, 2004 at 10:11 UTC
    I'm not having much luck with the alternation code.

    I think it'd have problems with 5 or 6 backslashes. What's more, since it matches the backslashes, it'd trim them off the end of the resulting split strings.

    I'd better try that out and update...

    DB<9> x @a= split /(?:\\\\X)|(?:(?<!\\)X)/, $escaped_str 0 '' 1 'a\\X\\\\b' 2 'c' 3 'd' DB<10> p $escaped_str Xa\X\\bXc\\XdX
    Yes, the c gets striped