$s='|xx-xxx-xxxxx-xxx x/xx|xx-xxxx-xxx-xx-xxxx-xx-xx|'; # in one line, if not one command ($a, $b)=$s=~/^\|?(\w+-\w+-\w+-)(.+)/; $b=~s/[-| ]/,/g; $s=$a . $b; print $s # xx-xxx-xxxxx-xxx,x/xx,xx,xxxx,xxx,xx,xxxx,xx,xx,