Hi Abhishek
You can try using Addressable gem for your requirement .
Step 1 : Install Addressable gem with the following command .
$sudo gem install addressable
Step 2 : Will be explaining with IRB u can try and integrate with
your rails application .
$ irb
> require 'rubygems'
> require 'addressable/uri'
> uri = Addressable::URI.parse("http://google.com")
=> #<Addressable::URI:0xfdb9aee5c URI:http://google.com>
Step 3 : You can extract only the host with the following command
> uri.host
=> "google.com"
There are many other different options which you can explore
http://addressable.rubyforge.org/api/classes/Addressable/URI.html
Hope this helps !
Best regards,
Srinivas Iyer
http://talkonsomething.com
http://twitter.com/srinivasiyermv
On 11/12/09, Conrad Taylor <[email protected]> wrote:
> On Wed, Nov 11, 2009 at 10:25 PM, Abhishek shukla
> <[email protected]>wrote:
>
>> Hello Friends,
>> I need to write a regular expression which will extract and return the
>> domain name.
>>
>> for example
>> if a user parse any of the below mention url it should save only "foo.com"
>>
>> http://www.foo.com/
>> http://www.foo.com/something
>> http://foo.com/
>> https://something.foo.com/
>>
>> Thanks for any help..
>>
>> Thanks
>> abhis
>>
>>
>>
> require 'uri'
>
> urls = [ "http://www.foo.com/", "http://www.foo.com/something", "
> http://foo.com/", "https://something.foo.com/" ]
>
> urls.each { |url| puts URI::parse( url ).host.split( "." )[-2,2].join(".") }
>
> Good luck,
>
> -Conrad
>
>
>> >
>>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---