Remove "http://" and "https://" from a string

See the String#sub(...) method.

Also, consider using the %r{...} literal notation for Regexp objects so that forward-slashes (/) are easier to recognize:

def trim_url(str)
  str.sub %r{^https?:(//|\\\\)(www\.)?}i, ''
end

trim_url 'https://www.foo.com' # => "foo.com"
trim_url 'http://www.foo.com'  # => "foo.com"
trim_url 'http://foo.com'      # => "foo.com"
trim_url 'http://foo.com'      # => "foo.com"

Here is what each part of the regular expression means:

%r{^https?:(//|\\\\)(www\.)?}
#  │├──┘├┘│├───────┘ ├─┘├┘ └── everything in the group (...), or nothing.
#  ││   │ ││         │  └── the period character "."
#  ││   │ ││         └── the letters "www".
#  ││   │ │└── the characters "//" or "\\".
#  ││   │ └── the colon character ":".
#  ││   └── the letter "s", or nothing.
#  │└── the letters "http".
#  └── the beginning of the line.

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"

Tags:

Ruby

Regex