in reply to Re: Replacing string with its length in Regex
in thread Replacing string with its length in Regex

even simpler:
$r =~ s/(^_+|_+$)//g; $r =~ s/_+/,/g; $r =~ s/(X+)/length$1/ge;
  p