本文介绍了在 Ruby 中使用正则表达式提取两个标签之间的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个包含 html 标签的字符串:
Let say I have this string which contains html a tag:
<a href="abgeordnete-1128-0----w8397.html" class="small_link">Berlin-Treptow-Köpenick</a>
如何在 ruby 中使用正则表达式提取Berlin-Treptow-Köpenick"的文本?
How do I use regex in ruby to extract the text of "Berlin-Treptow-Köpenick" ?
谢谢!:)
推荐答案
您可以使用:
html = '<a href="abgeordnete-1128-0----w8397.html" class="small_link">Berlin-Treptow-Köpenick</a>'
html.match(/>(.*)</)[1]
#=> "Berlin-Treptow-Köpenick"
当您的 html 部分变得更复杂时,我建议您查看 nokogiri 之类的库.
When your html partial get more complex then I would recommend looking libraries like nokogiri.
这篇关于在 Ruby 中使用正则表达式提取两个标签之间的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!