in reply to Variable-width negative lookbehind

How about the lovely and talented /e?

$string =~ s/(.)X+/$1 eq 'Y' ? YX : $1/eg;