سؤال

أنا أستعمل نوكوجيري (Rubygem) بحث CSS للبحث عن مؤكد <div> داخل HTML الخاص بي. يبدو أن بحث CSS من Nokogiri لا يحب Regex. أود التبديل إلى نوكوجيري xpath ابحث لأن هذا يبدو أنه يدعم Regex في سلاسل البحث.

كيف يمكنني تنفيذ بحث CSS (Pseudo) المذكور أدناه في بحث 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
هل كانت مفيدة؟

المحلول

مايك داليسيو (نصف مطوري نوكوجيري الأساسيين) أعطاني إجابة على #nokogiri (irc.freenode.net). يبدو أنه لا يدعم Nokogiri CSS أو Xpath Search مطابقة Regex. هذا هو حله حول كيفية البحث عن تعبيرات منتظمة مع Nokogiri:

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>
      <div class="bar" 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]+"
# full regex to search for in node ids
full_regex = %r(eq-#{my_eq}_bl-#{my_bl})

filter_by_id = Class.new do
  attr_accessor :matches

  def initialize(regex)
    @regex = regex
    @matches = []
  end

  def filter(node_set)
    @matches += node_set.find_all { |x| x['id'] =~ @regex }
  end
end.new(full_regex)

value.css("div.foo:filter()", filter_by_id)
filter_by_id.matches.each do |node|
  puts node
end

نصائح أخرى

نهج أبسط يعتمد على الإجابة أعلاه:

regex = /subject|header/
headers = doc.css("table td:nth-child(1) div").find_all do
  |h| h['class'] =~ regex
end

شكرا لنشر هذا السؤال.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top