in reply to Splitting on escapable delimiter

# Extract fields my @fields = /((?:[^#@]+|#.)*)/sg; # Remove seperators my $ff = 0; @fields = grep $ff^=1, @fields; # Unescape s/#(.)/$1/sg for @fields;

or use Text::CSV

Updated to remove empty elements were being placed in @fields.

Replies are listed 'Best First'.
Re^2: Splitting on escapable delimiter
by Roy Johnson (Monsignor) on Mar 28, 2008 at 18:24 UTC
    If you consume the separator, you don't have to filter it out. And if you put the escape regex first, you don't have to mention the # twice.
    my @fields = /((?:#.|[^@])*)\@?/sg;

    Caution: Contents may have been coded under pressure.
      Nope, that returns an extra (empty) field most of the time, and there's no way to know when. For example, 'a@b' incorrectly returns 3 fields, although 'a@' correctly returns 2.
        That's weird. I'm trying to figure out why mine takes another pass after encountering end of string (even if I specify that the separator can be end-of-string). Meanwhile, I'll point out that your method finds only one field in '@' (instead of two empty ones — if there's no newline after the @).

        Update: this seems to work:

        my @pieces = /(?:^|\G@)((?:#.|[^@])*)/sg;
        Still don't know why I couldn't get it to realize it had hit end of string. My guess is, if end-of-string is not required to match, it takes another shot to make sure nothing matches.

        Caution: Contents may have been coded under pressure.