slindsey3000 slindsey3000 - 1 month ago 8
Ruby Question

Pull all URLS out of this string in Ruby with scan method

I have searched far and wide but cannot find an answer that works. Please test out any links or "already answered here" responses. I have been looking for 3 hours.

Here is my string. Pull out all 6 URLS even if http(s) is not there.

"This string has some urls.For example it has Google.com and Google.com/stuff and then you can see Joe.mysite.co and http://pete.com these are all things that should be detected like https://wtf.net or http://mystuff.com/apathe/433"

Answer

Given s your string

s.scan(/((http(s)?:\/\/)?(\w*\.){0,1}(\w)+\.(co(m)?|net)+(\/(\w)*)*)/).map(&:first)
 => ["Google.com", "Google.com/stuff", "Joe.mysite.co", "http://pete.com", "https://wtf.net", "http://mystuff.com/apathe/433"] 
Comments