正则表达式删除 ruby 中网址的网页部分 [英] regex to remove the webpage part of a url in ruby
本文介绍了正则表达式删除 ruby 中网址的网页部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除 URL 的网页部分
I am trying to remove the webpage part of the URL
例如
www.example.com/home/index.html
到
www.example.com/home
感谢任何帮助.
谢谢
any help appreciated.
Thanks
推荐答案
如果您决定使用正则表达式并且您知道您的 URL 将非常简单,您可以使用 (.*)/.*
捕获 URL 中最后一个/之前的所有内容.
If your heart is set on using regex and you know that your URLs will be pretty straight forward you could use (.*)/.*
to capture everything before the last / in your 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"
这篇关于正则表达式删除 ruby 中网址的网页部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文