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