请原谅我的无知,我是Ruby的新手.
我知道如何搜索字符串,甚至是一个带正则表达式的单个文件:
str = File.read('example.txt')
match = str.scan(/[0-9A-Za-z]{8,8}/)
puts match[1]
Run Code Online (Sandbox Code Playgroud)
我知道如何在多个文件和目录中搜索静态短语
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
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何对多个文件和目录使用我的正则表达式.任何和所有的帮助非常感谢.