$ perl -le'my @x = ( "A", "A, B", "A, B, C" ); # 1a for my $test ( @x ) { $test =~ s/(.*),/$1 and/s; print $test; } ' A A and B A, B and C $ perl -le'my @x = ( "A", "A, B", "A, B, C" ); # 1b for my $test ( @x ) { $test =~ s/.*\K,/ and/s; print $test; } ' A A and B A, B and C $ perl -le'my @x = ( "A", "A, B", "A, B, C" ); # 2a for my $test ( @x ) { $test =~ s/,([^,]*)\z/ and$1/; print $test; } ' A A and B A, B and C $ perl -le'my @x = ( "A", "A, B", "A, B, C" ); # 2b for my $test ( @x ) { $test =~ s/,(?=[^,]*\z)/ and/; print $test; } ' A A and B A, B and C $ perl -le'my @x = ( "A", "A, B", "A, B, C" ); # 3 for my $test ( @x ) { $test = reverse $test; $test =~ s/,/dna /; $test = reverse $test; print $test; } ' A A and B A, B and C