If you really have an array (as you state) and it is an array of strings (I assume), for example
foo = [ "hello", "42 cats!", "yöwza" ]
then I can imagine that you either want to update each row in the array with a new value, or you want the modified array to contain only certain rows.
If the first (you want to "clear" each line with an array), you can do one of the following:
foo.each{ |s| s.gsub! /\p{^Alnum}/, '' }
If the latter (you want to select a subset of strings where each matches your criteria for storing only alphanumeric characters), you can use one of them:
# Select only those strings that contain ONLY alphanumerics bar = foo.select{ |s| s =~ /\A\p{Alnum}+\z/ } #=> [ "hello", "yöwza" ] # Shorthand method for the same thing bar = foo.grep /\A\p{Alnum}+\z/ #=> [ "hello", "yöwza" ]
In Ruby, regular expressions of the form /\A………\z/
require matching the entire string, since \A
binds the regular expression to the beginning of the string and binds \z
to the end.
source share