s{(\\*)(\s+)$}{ if( $1) { if( length( $1) % 2) { $1 . substr( $2, 0, 1) } else { $1 } } else { '' } }e; #### #!/usr/bin/perl -w use strict; use Test::More qw(no_plan); while( ) { my( $input, $expected)= m{^'(.*?)'\s*=>\s*'(.*?)'}; #(my $trimmed= $input)=~ s{^((\\.|.)*?)\s*$}{defined $1 ? $1 : ''}e; (my $trimmed= $input)=~ s{(\\*)(\s+)$}{ if( $1) { if( length( $1) % 2) { $1 . substr( $2, 0, 1) } else { $1 } } else { '' } }e; is( $trimmed, $expected, "'$input'"); } __DATA__ '' => '' 't' => 't' 't ' => 't' 't\ ' => 't\ ' 't\ ' => 't\ ' 't\ \ ' => 't\ \ ' 't\\\ ' => 't\\\ ' 't\\ ' => 't\\'