ruby
  • nokogiri
  • hpricot
  • 2009-10-12 15 views 23 likes 
    23

    Gegeben:Wie mache ich eine Regex-Suche in Nokogiri nach Text, der einem bestimmten Anfang entspricht?

    require 'rubygems' 
    require 'nokogiri' 
    value = Nokogiri::HTML.parse(<<-HTML_END) 
    "<html> 
    <body> 
        <p id='para-1'>A</p> 
        <div class='block' id='X1'> 
        <h1>Foo</h1> 
        <p id='para-2'>B</p> 
        </div> 
        <p id='para-3'>C</p> 
        <h2>Bar</h2> 
        <p id='para-4'>D</p> 
        <p id='para-5'>E</p> 
        <div class='block' id='X2'> 
        <p id='para-6'>F</p> 
        </div> 
    </body> 
    </html>" 
    HTML_END 
    

    ich so etwas tun will, was ich in Hpricot tun kann:

    divs = value.search('//div[@id^="para-"]') 
    
    1. Wie für Elemente in XPath Stil ein Muster Such ich tun?
    2. Wo finde ich die Dokumentation, die mir hilft? Ich habe das in den Rdocs nicht gesehen.
    +0

    PSA: Für diejenigen, komplexere regex versucht, dies ist wahrscheinlich, was Sie suchen: http://stackoverflow.com/questions/649963/ nokogiri-suche-nach-div-using-xpath – DreadPirateShawn

    Antwort

    64

    Verwenden Sie die XPath-Funktion starts-with:

    value.xpath('//p[starts-with(@id, "para-")]').each { |x| puts x['id'] } 
    
    +29

    Wow, Aaron selbst hat es gerade beantwortet! – khelll

    +1

    @khelll was ist so cool in Aaron? –

    +5

    Autor von Nokogiri und RoR-Kernteam-Mitglied. – khelll

    2

    Und einige docs Sie suchen:

    16
    divs = value.css('div[id^="para-"]') 
    
    +0

    das ist ein Lebensretter – Onichan

    1
    Nokogiri::XML::Node.send(:define_method, 'xpath_regex') { |*args| 
        xpath = args[0] 
        rgxp = /\/([a-z]+)\[@([a-z\-]+)~=\/(.*?)\/\]/ 
        xpath.gsub!(rgxp) { |s| m = s.match(rgxp); "/#{m[1]}[regex(.,'#{m[2]}','#{m[3]}')]" } 
        self.xpath(xpath, Class.new { 
        def regex node_set, attr, regex 
         node_set.find_all { |node| node[attr] =~ /#{regex}/ } 
        end 
        }.new) 
    } 
    

    Verbrauch:

    divs = Nokogiri::HTML(page.root.to_html). 
        xpath_regex("//div[@class~=/axtarget$/]//div[@class~=/^carbo/]") 
    

     Verwandte Themen

    • Keine verwandten Themen^_^