我使用Nokogiri<div>
(Rubygem) css 搜索在我的 html中查找某些内容。看起来 Nokogiri 的 css 搜索不喜欢正则表达式。我想切换到 Nokogiri 的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。
如何在 xpath 搜索中实现下面提到的(伪)css 搜索?
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