am new to ruby
using regular expression .how can i remove https and http and www from a string
server= http://france24.miles.com
server= https://seloger.com
from these sites i want to remove all http ,https and www
france24.miles.com
seloger.com
i used following code but it is not woking for me
server = server.(/^https?\:\/\/(www.)?/,'')
                sub(r'http\S+', '', my_string) . The re. sub() method will remove any URLs from the string by replacing them with empty strings.
server = server.(/^https?\:\/\/(www.)?/,'')
This didn't work, because you aren't calling a method of the string server. Make sure you call the sub method:
server = server.sub(/^https?\:\/\/(www.)?/,'')
Example
> server = "http://www.stackoverflow.com"
> server = server.sub(/^https?\:\/\/(www.)?/,'')
stackoverflow.com
As per the requirement if you want it to work with the illegal format http:\\ as well, use the following regex:
server.sub(/https?\:(\\\\|\/\/)(www.)?/,'')
                        Std-lib URI is dedicated for such kind of work. Using this would be simpler and may be more reliable
require 'uri'
uri = URI.parse("http://www.ruby-lang.org/")
uri.host
=> "www.ruby-lang.org"
uri.host.sub(/\Awww\./, '')
=> "ruby-lang.org"
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With