in reply to vertical regex in a matrix?

This was my attempt
my $string = <<EOF; xxxYYxxxYYxxx YY YY 4Y 4X 4Y 4Y YY YY EOF print "1: $string"; $string =~ s/ (?: (?<=x) | \G ) (Y) (?= Y* x) /x/gx; print "2: $string"; $string =~ s/ (?<= ^ (.*) Y .* $ ^ \1 ) (4) (?= .* $ ^ \1 Y .* $ ) /Y/mgx; print "3: $string";
The first part to match the horizontal parts works fine. The second part to match the verticals doens't work though for two reasong
  1. It is going to match the same string horizontally in each case up to the vertical replacement (which wasn't specified)
  2. Perl gave this message "variable length lookbehind not implemented" when I tried it ;-)