Sreeraj Sreeraj - 6 months ago 14
Ruby Question

Regular expression to explode the URLs

When I am trying to explode the url from one string, its not returning the actual URL. Please find the def I have used

def self.getUrlsFromString(str="")
url_regexp = /(?:http|https):\/\/[a-z0-9]+(?:[\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(?:(?::[0-9]{1,5})?\/[^\s]*)?/ix
url = str.split.grep(url_regexp)
return url
rescue Exception => e
DooDooLogger.log(e.message,e)
return ""
end


when I do
self.getUrlsFromString(" check this site...http://
lnkd.in/HjUVii")
it's returning


site...http://lnkd.in/HjUVii


Instead of


http://lnkd.in/HjUVii

Answer

It's because grep in Array class returns an array of every element for element === pattern, so

str.split.grep(/http/ix)

will return ["site...http://lnkd.in/HjUVii"] too.

You can try instead of

str.split.grep(url_regexp)

something like this:

url_regexp.match(str).to_s