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/"
You are not logged in. Please login or register.
Rails Forum - Ruby on Rails Help and Discussion Forum » Ruby Programming » Parsing URL from a string
Given this string:
message = "check out this site: http://www.somesite.net/"
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'
Last edited by oshuma (2007-10-24 05:30:58)
Maybe match the http part only, with an optional s parameter?
/http[s]?:\/\/\w/
Good point on the http[s] part. It seems to be working fine.
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
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
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"