Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我正在从中提取地址div.body h3 a。问题是,如果我只想要部分地址怎么办?例如,如果 html 读取:<a href="/usa/sale/100-happy-street">100 Happy Street #PH </a>
div.body h3 a
<a href="/usa/sale/100-happy-street">100 Happy Street #PH </a>
怎么说,我只想显示PH?
PH
anchor = doc.at('div.body h3 a') # the <a …>…</a> element link = anchor.text # "100 Happy Street #PH " last = link[ /#([^#]+)/, 1 ] # "PH"
这个正则表达式(与 Ruby on Rails 或 Nokogiri 无关)从字符串中最后一个字符串中提取所有文本#,假设至少有一个#. 你可以得到类似的结果last = link.split("#").last。
#
last = link.split("#").last