samedi 27 juin 2015

Using arrays in regular expressions (ruby)?

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)/)

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire