我试图删除URL正则表达式来删除一个网址的网页部分红宝石
。例如网页部分,
www.example.com/home/index.html
到
www.example.com/home
任何帮助表示赞赏。
谢谢
我试图删除URL正则表达式来删除一个网址的网页部分红宝石
。例如网页部分,
www.example.com/home/index.html
到
www.example.com/home
任何帮助表示赞赏。
谢谢
如果你的心脏上设置使用正则表达式,你知道你的网址,将是非常简单的,你可以使用(.*)/.*
前的最后捕捉到的一切/你的URL。
irb(main):007:0> url = "www.example.com/home/index.html"
=> "www.example.com/home/index.html"
irb(main):008:0> regex = "(.*)/.*"
=> "(.*)/.*"
irb(main):009:0> url =~ /#{regex}/
=> 0
irb(main):010:0> $1
=> "www.example.com/home"
也许/(.*)/.*$/? – parallelgeek 2012-04-21 23:02:46
这可能是一个好主意,不要在可能的情况下使用正则表达式。 You may summon Cthulhu。尝试使用属于标准库一部分的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"
+1网址都是不正规的,不能用正则表达式解析它们,使用URI LIB – clyfe 2010-09-30 10:52:29
Addressable :: URI是Ruby的另一个很好的URI模块,功能更全面。尽管如此,Ruby的内置URI应该足够用于此目的。 http://github.com/sporkmonger/addressable – 2010-09-30 14:48:09
http://addressable.rubyforge.org/是可寻址的主页面。 – 2010-09-30 14:55:45
irb(main):001:0> url="www.example.com/home/index.html"
=> "www.example.com/home/index.html"
irb(main):002:0> url.split("/")[0..-2].join("/")
=> "www.example.com/home"
尽管这在技术上有效,但会在不同的深度URL(/home/index.html vs /admin/users/index.html)上打破。这就是为什么URI.parse更好。 – 2010-09-30 12:45:21
@Jason:在什么情况下'0 ..- 2'会中断? – 2010-09-30 13:45:28
我重新读这个,你是对的,0 ..- 2应该始终工作。我仍然投票使用URI.parse。 – 2010-10-01 14:17:07
又见http://stackoverflow.com/questions/4716513/ruby-regular-expression-to-match-a-url – rogerdpack 2013-11-23 14:26:53