Topic: Parsing URL from a string

Given this string:

message = "check out this site: http://www.somesite.net/"

How would I strip out the URL, to end up with a string like "http://www.somesite.net/"

Re: Parsing URL from a string

This seems to work:

text = "some string with a link http://www.google.com"
url_regexp = /http:\/\/\w/
url = text.split.grep(url_regexp)
# url variable should now contain 'http://www.google.com'

The regular expression can probably be tighter.  Anyone know of a better way?

Last edited by oshuma (2007-10-24 05:30:58)

Re: Parsing URL from a string

Maybe match the http part only, with an optional s parameter?

/http[s]?:\/\/\w/

Re: Parsing URL from a string

Good point on the http[s] part.  It seems to be working fine.

Re: Parsing URL from a string

Hey guys,

this thread saved me hours. But what if you have more than one url in a string or a text?
I cannot make it work.

Here's my app helper for parsing urls from text:

def parse_url(text)
url_regexp = /http[s]?:\/\/\w/
url = text.split.grep(url_regexp).to_s
text.gsub(url,"<a href=\"" + url + "\" target=\"_blank\">" + url + "</a>").gsub("\n"," <br />")
end

But this only works for text with exactly one URL but not for two or more.

Any hints?
I already tried a little with url.each within the helper, but this lead to displaying only the URLs but the rest of the plain text was not displayed anymore although it was still in the database.

Cheers,
Heiko

Re: Parsing URL from a string

Rails has an auto_link() Helper

if you really want to do this on your own though, you can use \1 in a gsub statement to refer to the matched text:

"bla".gsub(/l/,'x\1x') 
#=> "bxlxa"

Thsi works for any number of matches