正则表达式收集最后一个/之后的所有内容 [英] Regular Expression to collect everything after the last /

查看:895
本文介绍了正则表达式收集最后一个/之后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的正则表达式,并且想知道如何用短语来表示最后一个/之后的所有内容.

I'm new at regular expressions and wonder how to phrase one that collects everything after the last /.

我正在提取Google GData使用的ID.

I'm extracting an ID used by Google's GData.

我的示例字符串是

http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123

ID为:p1f3JYcCu_cb0i0JYuCu123

哦,我正在使用PHP.

Oh and I'm using PHP.

推荐答案

这至少匹配以下项之一(非斜杠),并以字符串结尾:

This matches at least one of (anything not a slash) followed by end of the string:

[^/]+$


注意:

  • 没有理由,因为它不需要任何组-结果进入组0(匹配项本身).
  • 使用+(而不是*),以便如果最后一个字符是斜杠,则它不匹配(而不是匹配空字符串).
  • No parens because it doesn't need any groups - result goes into group 0 (the match itself).
  • Uses + (instead of *) so that if the last character is a slash it fails to match (rather than matching empty string).


但是,最有可能是更快,更简单的解决方案是使用您语言的内置字符串列表处理功能-即ListLast( Text , '/' )或同等功能.


But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' ) or equivalent function.

对于PHP,最接近的函数是 strrchr ,其工作原理如下: :

For PHP, the closest function is strrchr which works like this:

strrchr( Text , '/' )

这包括结果中的斜线-根据下面的Teddy的评论,您可以使用 substr :

This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:

substr( strrchr( Text, '/' ), 1 );

这篇关于正则表达式收集最后一个/之后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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