I am trying to pass regular expression to process a file line by line. The regular expression works fine if I hard code it in the code, like this.
File.foreach(filename).with_index do |line, line_num|
md5 = line.scan(/[0-9a-f]{32}/i)
puts md5
end
This works wonderful and I can see every line that has a MD5 hash on it printed. Now, the problem comes when I try to pass the regular expression to match md5 hashes as a script argument like:
ruby md5.rb -h "/[0-9a-f]{32}/i"
options = {}
OptionParser.new do |opts|
opts.on('-h', '--hash "<hash regex>"', 'Hash Regex') { |v| options[:hash] = v }
end.parse!
hash = options[:hash]
File.foreach(filename).with_index do |line, line_num|
md5 = line.scan(hash)
puts md5
end