25 Jun 2010

Ruby 正規表現入門ドリルの練習問題の解答

前回の練習問題の解答です。

1.
d, m_s, y = m.values_at 1,2,3

2.
>> dirs_files.collect{|v| v unless /(^\.\.)|(^\.)/.match(v) }.compact
=> ["app", "Capfile", "config", "coverage", "db", "doc", "features", "lib", "log", "public", "Rakefile", "README", "rerun.txt", "script", "spec", "tmp", "vendor"]

3.
puts File.read("test.html").match(/<title>/).post_match.match(/<\/title>/).pre_match
puts File.read("test.html").match(/<title>(.+)<\/title>/m)[1]