#!/usr/bin/perl -w use strict; use Smart::Comments; my $string = '423RY75Y69827EC67592C78657N965R345U299M'; my $correct = '423R_Y75Y_69827E_C67592C_78657N965R_345U_299M'; $string=~s/([A-MO-Za-mo-z])([\dN]+)\1/_$1$2$1_/g; ### $string # is now '423R_Y75Y_69827E_C67592C_78657N965R345U299M' # now i have troubles.. # the idea is to match right to left, and if after (the last digit # or N char), place an underscore, unless of course, that char is the # char we started with! $string=~s/([^\dN\3])([\dN]+)([A-MO-Za-mo-z])/$1_$2$3/g; ### $string # is now '423R_Y_75Y__69827E_C_67592C__78657N965R345U_299M' # wow, seems to have done the reverse of what i wanted!