gpt4 book ai didi

ruby - 如何使用 ruby​​ 正则表达式从标签中提取 href?

转载 作者:数据小太阳 更新时间:2023-10-29 08:15:43 27 4
gpt4 key购买 nike

我有这样声明的链接:

link = "<a href=\"https://www.congress.gov/bill/93rd-congress/house-bill/11461\">H.R.11461</a>"

问题是如何使用正则表达式只提取 href 值?

谢谢!

最佳答案

如果你想解析HTML,你可以使用Nokogiri gem 而不是使用正则表达式。这要容易得多。

例子:

require "nokogiri"

link = "<a href=\"https://www.congress.gov/bill/93rd-congress/house-bill/11461\">H.R.11461</a>"

link_data = Nokogiri::HTML(link)

href_value = link_data.at_css("a")[:href]

puts href_value # => https://www.congress.gov/bill/93rd-congress/house-bill/11461

关于ruby - 如何使用 ruby​​ 正则表达式从标签中提取 href?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33245198/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com