in reply to replace first occurance if it doesn't already match

Something like this perhaps?

#!/usr/bin/env perl use strict; use warnings; use Test::More; my @rows = ( { have => 'V12345:name_test', want => 'V12345:name_test' }, { have => 'V12345_name_test', want => 'V12345:name_test' }, ); plan tests => scalar @rows; for my $row (@rows) { my $out = $row->{have}; $out =~ s/(\d)_/$1:/; is ($out, $row->{want}); }

How well this regex works depends on how true your sample dataset is. You can amend @rows and the regex until you have covered all the possibilities you require. See also How to ask better questions using Test::More and sample data.

Addendum: I see that toolic has used precisely the same substitution in his answer while I was constructing this. That bodes well for it as a solution to your specific question.