URI.extract声称这样做,但它不处理匹配的括号:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
在不破坏带括号的 URL(用户喜欢使用)的情况下从文本中提取 URL 的最佳方法是什么?
URI.extract声称这样做,但它不处理匹配的括号:
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
在不破坏带括号的 URL(用户喜欢使用)的情况下从文本中提取 URL 的最佳方法是什么?
如果 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 /\(([^\)]*)\)/
在使用这个之前
>> URI.extract("text here (http://foo.example.org/bla) and here")
=> ["http://foo.example.org/bla)"]
你需要添加这个
require 'uri'
您可以使用此正则表达式从字符串中提取 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)