如何从文本中提取 URL [英] How to extract URLs from text
本文介绍了如何从文本中提取 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从 Ruby 中的纯文本文件中提取所有 URL?
How do I extract all URLs from a plain text file in Ruby?
我尝试了一些库,但在某些情况下它们会失败.最好的方法是什么?
I tried some libraries but they fail in some cases. What's the best way?
推荐答案
哪些案例失败了?
根据库regexpert,可以使用
regexp = /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix
然后对文本执行scan
.
似乎正则表达式支持空字符串.只需删除初始 (^$)
即可完成
Seems like the regexp supports the empty string. Just remove the initial (^$)
and you're done
这篇关于如何从文本中提取 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文