Nokogiri (Rubygem) css 検索を使用し<div>
て、html 内の特定のものを探します。Nokogiri の css 検索は正規表現が苦手なようです。これは検索文字列で正規表現をサポートしているようなので、Nokogiri のxpath検索に切り替えたいと思います。
下記の (疑似) css 検索を xpath 検索に実装するにはどうすればよいですか?
require 'rubygems'
require 'nokogiri'
value = Nokogiri::HTML.parse(<<-HTML_END)
"<html>
<body>
<p id='para-1'>A</p>
<p id='para-22'>B</p>
<h1>Bla</h1>
<p id='para-3'>C</p>
<p id='para-4'>D</p>
<div class="foo" id="eq-1_bl-1">
<p id='para-5'>3</p>
</div>
</body>
</html>"
HTML_END
# my_block is given
my_bl = "1"
# my_eq corresponds to this regex
my_eq = "\/[0-9]+\/"
# FIXME The following line should be changed to an xpath search.
if my_div = value.css("div#eq-#{my_eq}_bl-#{my_bl}.foo").first
# doing some stuff with the <p> inside the div
end