How do I get the last segment of URL using regular

2019-02-13 12:07发布

问题:

I have a URL:

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

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

Please note that the URL could just be:

www.domain.com/last/ 

Or:

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

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

Update

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

i.e.

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

should return last. Whereas:

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

should not return a match.

回答1:

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.



回答2:

This regex (a slightly modified version of Joseph's answer), should give you the last segment, minus ending slash.

([^/]+)/?$

Your result will be the first capture group.



回答3:

This should do the trick:

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

With a (?=lookahead) you won't get the last slash.

[^/]+ Looks for at least one character that is not a slash (as many as possible). (?=/?^|^) makes sure that the next part of the string is a / and then the end of string or just end of string.

Matches match in /one/two/match, '/one/two/match/'.



回答4:

The last slash might be optional. Right?

How about something like this:

$url =~ m|([^/]+)/?$|;
my $end_of_url = $1;

The $ on the end anchors the regular expression to the end of the string. The [^/] means anything that's not a slash and the + after means I want one or more things that are not slashes. Notice that this is in a capture group which are marked with parentheses.

I end the regular expression with /? which means that there may or may not be a slash on the very end of the string. I've put my regular expression between m| and |, so I can use forward slashes without having to constantly escape them.

The last part of the URL is now in $1 and I can set my own scalar variable to save this result.