$string =~ s/(.)X+/$1 eq 'Y' ? YX : $1/eg;
In reply to Re: Variable-width negative lookbehind by delirium in thread Variable-width negative lookbehind by dragonchild