re = /(?'group1'[\d])/m
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'
subst = '$1 & '
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
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 Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html