从字符串中删除"http://"和"https://"

Psl*_*Psl 8 ruby regex

是新的使用正则表达式我.怎么可以删除红宝石httpshttpwww从字符串

server= http://france24.miles.com
server= https://seloger.com
Run Code Online (Sandbox Code Playgroud)

从这些网站我想删除所有 http ,https and www

france24.miles.com
seloger.com
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码,但它不是为了我

server = server.(/^https?\:\/\/(www.)?/,'')
Run Code Online (Sandbox Code Playgroud)

Mar*_*cny 12

server = server.(/^https?\:\/\/(www.)?/,'')
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为您没有调用字符串的方法server.确保调用sub方法:

server = server.sub(/^https?\:\/\/(www.)?/,'')
Run Code Online (Sandbox Code Playgroud)

> server = "http://www.stackoverflow.com"
> server = server.sub(/^https?\:\/\/(www.)?/,'')
stackoverflow.com
Run Code Online (Sandbox Code Playgroud)

根据要求,如果您希望它http:\\也使用非法格式,请使用以下正则表达式:

server.sub(/https?\:(\\\\|\/\/)(www.)?/,'')
Run Code Online (Sandbox Code Playgroud)


Bil*_*han 6

Std-lib URI专门用于此类工作.使用它会更简单,也可能更可靠

require 'uri'

uri = URI.parse("http://www.ruby-lang.org/")

uri.host
#=> "www.ruby-lang.org"

uri.host.gsub('\Awww\.', '')
#+> "ruby-lang.org"
Run Code Online (Sandbox Code Playgroud)


mae*_*ics 6

String#sub(...)方法.

另外,请考虑使用%r{...}Regexp对象文字符号,以便/更容易识别正斜杠():

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"
Run Code Online (Sandbox Code Playgroud)

以下是正则表达式的每个部分的含义:

%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.
Run Code Online (Sandbox Code Playgroud)

  • @MartinKonecny:谢谢,我只是复制/粘贴[UTF-8盒子绘图字符](http://en.wikipedia.org/wiki/Box-drawing_character). (2认同)