如何使用PCRE获取最后一段网址?

NiL*_*iLL 0 php pcre

我有一个网址, - " http://example.com/sales/view/id/705"我需要得到最后一段(705).

我怎么能用PCRE做到这一点?

Eug*_*ash 6

这应该在Perl中完成:

my ($last) = $url =~ /([^\/]+)\z/;
Run Code Online (Sandbox Code Playgroud)

但我宁愿使用该URI模块:

my $last = (URI->new($url)->path_segments)[-1];
Run Code Online (Sandbox Code Playgroud)

  • 如果URL有查询或片段,你的正则表达式可能会失败,这表明为什么使用[URI](http://search.cpan.org/perldoc?URI)是明智之举.+1提及模块. (3认同)