如果我有这个字符串:
"<div class='name-and-date'><strong>Bartholome Hilpert MD - Dec 21,
12:38 PM Eastern</div></strong><div class='note-contents'>-
another</div><div> Attachment: <p class='attachment'>N/A</p></div></span>"
我如何获取这个值:
N/A
如您所见,我想扫描 <p class='attachment'>
的值是多少?
尝试:
conversation.scan(/<p class='attachment'>/)
但这并没有得到 p 标签中的值。
最佳答案
如果您想经常这样做,我会考虑使用 HTML 解析器,例如 Nokogiri因为为每一个这样的需求编写正则表达式是痛苦的。
require 'nokogiri'
html = Nokogiri::HTML("<div class='name-and-date'><strong>Bartholome Hilpert MD - Dec 21, 12:38 PM Eastern</div></strong><div class='note-contents'>- another</div><div> Attachment: <p class='attachment'>N/A</p></div></span>")
html.at_css('p.attachment').text # => "N/A"
关于ruby-on-rails - 从字符串中捕获字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47930374/