1

URI.extract声称这样做,但它不处理匹配的括号:

>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]

在不破坏带括号的 URL(用户喜欢使用)的情况下从文本中提取 URL 的最佳方法是什么?

4

3 回答 3

0

如果 URL 始终由括号绑定,则正则表达式可能是更好的解决方案。

text = "text here (http://foo.example.org/bla) and here and here is (http://yet.another.url/with/parens) and some more text"
text.scan /\(([^\)]*)\)/
于 2010-06-08T03:42:06.053 回答
-1

在使用这个之前

>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]

你需要添加这个

require 'uri'
于 2013-02-08T05:07:41.203 回答
-1

您可以使用此正则表达式从字符串中提取 URL

"some thing http://abcd.com/ and http://google.com are great".scan(/(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:(?::[0-9]{1,5})?\/[^\s]*)?/ix)
于 2013-02-08T05:18:13.163 回答