正则表达式删除红宝石中网址的网页部分

anu*_*uya 0 ruby regex

我正在尝试删除URL的网页部分

例如,

www.example.com/home/index.html 
Run Code Online (Sandbox Code Playgroud)

www.example.com/home 
Run Code Online (Sandbox Code Playgroud)

任何帮助赞赏.
谢谢

And*_*imm 9

在可能的情况下不使用正则表达式可能是个好主意.你可以召唤克苏鲁.尝试使用URI属于标准库的库.

require "uri"
result = URI.parse("http://www.example.com/home/index.html")
result.host # => www.example.com
result.path # => "/home/index.html"
# The following line is rather unorthodox - is there a better solution?
File.dirname(result.path) # => "/home"
result.host + File.dirname(result.path) # => "www.example.com/home"
Run Code Online (Sandbox Code Playgroud)