samedi 27 juin 2015

Using arrays in regular expressions?

Does anyone know if there is a way to use an array in a regular expression? suppose I want to find out if somefile.txt contains one of an array's elements. Obviously the code below doesn't work, but is there something similar that does work?

array = [thing1 thing2 thing3]
file = File.open("somefile.txt")

file.each_do |line|
if /array/.match(line)
puts line
end

Basically I've got a big list of words to search for, and I'd like to avoid something like this:

($somefile =~ /(thing1|thing2|thing3)/)

Aucun commentaire:

Enregistrer un commentaire