use strict;
my $str = '101011010
110101101
111010110
111101011
011110101
101111010
110111101
011011110
001101111
100110111
110011011
111001101
111100110
011110011
001111001
100111100
010011110
001001111
100100111
010010011
001001001
000100100
000010010
000001001
000000100
000000010
100000001';
my $regex = qr/(?'group1'[\d])/mp;
my $subst = '$1 & ';
my $result = $str =~ s/$regex/$subst/rg;
print "The result of the substitution is' $result\n";
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html