解决了。要把文本读出来存储为字段,然后再来做匹配 str=IO.read("c:/httpd.conf") ss=str.scan(/\n\s+DirectoryIndex[\s\w\d\.\/_-]+<\/VirtualHost>/m) ss.length.times{|tt| puts "这个是第#{tt+1}个匹配项:\n#{ss[tt]}"}