in reply to Handling escapes while splitting lines

my( $field, $value )= /((?:[^:\\]+|\\.)*):(.*)/s;

- tye        

Replies are listed 'Best First'.
Re^2: Handling escapes while splitting lines
by mikecarlton (Novice) on Jan 19, 2005 at 05:35 UTC
    Aha -- that does it. I like the approach much better (matching non-backslash and colon or 2-character escape sequences).

    Running some quick test code:

    my $re = shift or die "re expected\n"; while (<>) { chomp; my ($field, $value) = ($_ =~ $re); print "'$_' -> ('$field', '$value')\n"; }
    We see that it handles all these cases correctly:
    'a:b:c' -> ('a', 'b:c') 'a\:b:c' -> ('a\:b', 'c') 'a\\:b:c' -> ('a\\', 'b:c') 'a\\\:b:c' -> ('a\\\:b', 'c') 'a:' -> ('a', '') ':b' -> ('', 'b')
    The only thing it doesn't handle perfectly is the invalid input case with an escaped colon but no delimiter:
    'a\:b' -> ('', 'b')
    Notice that this is indistinguishable from the ':b' input. The fix is simple though, just anchor the start of the match:
    ^((?:[^:\\]+|\\.)*):(.*)
    For my use this works better, as the match fails, telling me there was no field delimiter present.

    Thanks
    --mike