in reply to Variable-width negative lookbehind
$string=~/([^Y]+)X/$1/g; [download]
$string =~ s/(?<=[^Y])X+//g; [download]