找到div中的所有包含元素 - Ruby

时间:2017-12-18 18:58:35

标签: ruby-on-rails ruby

我有这个字符串:

STRING:

"<div class='break'><div class='name-and-date'><strong>Mr. Talon 
 Williamson - Dec 18,  1:47 PM Eastern</div></strong><div class='note-
 contents'>- wrong</div></div><div class='break'><div class='name-and-
 date'><strong>Mr. Talon Williamson - Dec 18,  1:47 PM Eastern</div>
 </strong><div class='note-contents'>- Wrong again</div></div><div 
 class='break'><div class='name-and-date'><strong>Mr. Talon Williamson 
 - Dec 18,  1:47 PM Eastern</div></strong><div class='note-contents'>- 
 okay what is the matter with you.</div></div><div class='break'><div 
 class='name-and-date'><strong>Mr. Talon Williamson - Dec 18,  1:50 PM 
 Eastern</div></strong><div class='note-contents'>- Bro!</div></div>"

在这个字符串中有一个div,其类为&#34; break&#34;。我试图删除该div及其包含的所有内容。

所以从那个字符串我希望它返回:

希望这次回归:

"<div class='break'><div class='name-and-date'><strong>Mr. Talon 
 Williamson - Dec 18,  1:47 PM Eastern</div></strong><div class='note-
 contents'>- wrong</div></div><div class='break'><div class='name-and-
 date'><strong>Mr. Talon Williamson - Dec 18,  1:47 PM Eastern</div>
 </strong><div class='note-contents'>- Wrong again</div></div><div 
 class='break'><div class='name-and-date'><strong>Mr. Talon Williamson 
 - Dec 18,  1:47 PM Eastern</div></strong><div class='note-contents'>- 
 okay what is the matter with you.</div></div>"

当前的尝试:

compliance_string.scan(/<div class='break'>/).last

但是这只会返回div而不是所有包含元素及其内容。

2 个答案:

答案 0 :(得分:5)

使用nokogiri gem

require 'nokogiri'

html_doc = Nokogiri::HTML(your_html_string)
html_doc.css('.break:last-child').to_s
=> "<div class=\"break\">\n<div class=\"name-and-date\"><strong>Mr. Talon Williamson - Dec 18,  1:50 PM \n Eastern</strong></div>\n<div class=\"note-contents\">- Bro!</div>\n</div>"

答案 1 :(得分:1)

试试这个

require 'nokogiri'

str = "<div class='break'><div class='name-and-date'><strong>Mr. Talon 
 Williamson - Dec 18,  1:47 PM Eastern</div></strong><div class='note-
 contents'>- wrong</div></div><div class='break'><div class='name-and-
 date'><strong>Mr. Talon Williamson - Dec 18,  1:47 PM Eastern</div>
 </strong><div class='note-contents'>- Wrong again</div></div><div 
 class='break'><div class='name-and-date'><strong>Mr. Talon Williamson 
 - Dec 18,  1:47 PM Eastern</div></strong><div class='note-contents'>- 
 okay what is the matter with you.</div></div><div class='break'><div 
 class='name-and-date'><strong>Mr. Talon Williamson - Dec 18,  1:50 PM 
 Eastern</div></strong><div class='note-contents'>- Bro!</div></div>"

page = Nokogiri::HTML(str)
last_div = page.xpath("//div[@class='break'][last()]")
puts last_div.to_s