Please forgive my ignorance, I am new to Ruby.
I know how to search a string, or even a single file with a regular expression:
str = File.read('example.txt')
match = str.scan(/[0-9A-Za-z]{8,8}/)
puts match[1]
I know how to search for a static phrase in multiple files and directories
pattern = "hello"
Dir.glob('/home/bob/**/*').each do |file|
next unless File.file?(file)
File.open(file) do |f|
f.each_line do |line|
puts "#{pattern}" if line.include?(pattern)
end
end
end
I can not figure out how to use my regexp against multiple files and directories. Any and all help is much appreciated.
Well, you’re quite close. First make pattern a Regexp object:
Or if you are trying to make a Regexp from a String (like passed in on the command line), you might try:
Now when you are searching,
lineis a String. You can usematchorscanto get the results of it matching against your pattern.