正则表达式删除"http://"之前的文本?

dMi*_*Mix 1 ruby regex

我有一个ruby应用程序从字符串解析一堆URL:

@text = "a string with a url http://example.com"

@text.split.grep(/http[s]?:\/\/\w/)

@text[0] = "http://example.com"
Run Code Online (Sandbox Code Playgroud)

这很好用^^

但有时URL在HTTP://之前有文本

@text = "What's a spacebar? ...http://example.com"

@text[0] = "...http://example.com"
Run Code Online (Sandbox Code Playgroud)

是否有正则表达式可以在字符串中选择"http://"之前的文本,以便我可以将其删除?

Oll*_*lly 10

也许更好的方法来实现相同的结果是使用URI标准库.

require 'uri'
text = "a string with a url http://example.com and another URL here:http://2.example.com and this here"
URI.extract(text, ['http', 'https'])
# => ["http://example.com", "http://2.example.com"]
Run Code Online (Sandbox Code Playgroud)

文档:URI.extract


Pes*_*sto 6

分裂然后grepping是一种奇怪的方法.为什么不使用String#scan:

@text = "a string with a url http://example.com"
urls = @text.scan(/http[s]?:\/\/\S+/)
url[0]  # => "http://example.com"
Run Code Online (Sandbox Code Playgroud)