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 |