my $nth = 4 -1 ; #replath 4th , to | my $str = 'a,bb,ccc,dddd,eeeee,ffffff'; $str =~ s{ (?: , [^,]*){$nth} \K , }{|}xms;