如何使用正则表达式获取 URL 的最后一段 [英] How do I get the last segment of URL using regular expressions

查看:107
本文介绍了如何使用正则表达式获取 URL 的最后一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址:

www.domain.com/first/second/last/

如何获得斜线之间的最后一个词?即 last 使用正则表达式?

How do I get the last term between slashes? i.e. last using regular expressions?

请注意,URL 可以是:

Please note that the URL could just be:

www.domain.com/last/ 

或者:

www.domain.com/first/second/third/fourth/last/

我需要提取最后一个术语,以便在使用 PERL 和 REGEX 的 Zeus 服务器的请求重写模块中使用.

I need to extract this last term for use in the Zeus Server's Request Rewrite module which uses PERL and REGEX.

在实现了一些答案之后,我才意识到我只需要对某个目录中的 URL 进行匹配.

After implementing some answers, I have just realized that I need this match to be made only on URLs in a certain directory.

www.domain.com/directory/first/second/last/ 

应该返回last.鉴于:

www.domain.com/first/second/last/ 

不应返回匹配项.

推荐答案

这是一个简单的正则表达式:

Here's a simple regex:

[^/]+(?=/$|$)

应该匹配你扔给它的任何东西.

Should match anything you throw at it.

如果要查看特定目录,请使用:

If you want to look in a particular directory, use this:

/directory.*/([^/]+)/?$

您的结果将在第一个捕获组中.

and your result will be in the first capture group.

这篇关于如何使用正则表达式获取 URL 的最后一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆