$_ = "foobarbazquxquux"; my $c=0; s( (||.) ) { my $s = $1; $s =~ m!?span>! ? $s : $s . (++$c % 4 ? '' : "\n"); }sxge; print; print "\n";