Some examples for matching a numberstring that contains 332:
string = "12332231 3321705" string.scan(/\B332\B/) { |x| puts x } string.scan(/\w+?\B332\B\w+/) { |x| puts x } # \d for \w is also possible here a = string.gsub(/(\w+?)\B332\B(\w+)/) { $1 << "9999999" << $2 } # replaces 332 by 9999999 puts a