in reply to Real global substitution, what's the best way?

I came up with this:

my $beg= ""; # Beginning of new string my $end= ""; # Ending of new string my $prev= ""; # Previous token my $str= "'a1b1a2b1b2a3b1b2b3'db1b2"; for my $sub ( split /([ab]\d+)/, $str ) { next if "" eq $sub; if( $prev =~ /^a/ && $sub =~ /^b/ ) { $end .= "($sub)"; } else { $beg .= $sub; } $prev= $sub; } print "$beg$end\n";

        - tye (but my friends call me "Tye")