php 正则表达式收集最后一个 / 之后的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1150559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular Expression to collect everything after the last /
提问by Hellonearthis
I'm new at regular expressions and wonder how to phrase one that collects everything after the last /.
我是正则表达式的新手,想知道如何表达一个在最后一个/.
I'm extracting an ID used by Google's GData.
我正在提取 Google 的 GData 使用的 ID。
my example string is
我的示例字符串是
http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123
Where the ID is: p1f3JYcCu_cb0i0JYuCu123
身在哪里: p1f3JYcCu_cb0i0JYuCu123
Oh and I'm using PHP.
哦,我正在使用 PHP。
回答by Peter Boughton
This matches at least one of (anything not a slash) followed by end of the string:
这至少匹配(任何非斜杠)后跟字符串结尾之一:
[^/]+$
Notes:
笔记:
- 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).
- 没有括号,因为它不需要任何组 - 结果进入组 0(匹配本身)。
- 使用
+(而不是*) 以便如果最后一个字符是斜杠则它无法匹配(而不是匹配空字符串)。
But, most likely a faster and simpler solutionis to use your language's built-in string list processing functionality - i.e. ListLast( Text , '/' )or equivalent function.
但是,最有可能更快更简单的解决方案是使用您语言的内置字符串列表处理功能 - 即ListLast( Text , '/' )或等效功能。
For PHP, the closest function is strrchrwhich works like this:
对于 PHP,最接近的函数是strrchr,其工作方式如下:
strrchr( Text , '/' )
This includes the slash in the results - as per Teddy's comment below, you can remove the slash with substr:
这包括结果中的斜杠 - 根据下面泰迪的评论,您可以使用substr删除斜杠:
substr( strrchr( Text, '/' ), 1 );
回答by Gumbo
Generally:
一般来说:
/([^/]*)$
The data you want would then be the match of the first group.
您想要的数据将是第一组的匹配项。
Edit???Since you're using PHP, you could also use strrchrthat's returning everything from the last occurence of a character in a string up to the end. Or you could use a combination of strrposand substr, first find the position of the last occurence and then get the substring from that position up to the end. Or explodeand array_pop, split the string at the /and get just the last part.
编辑???由于您使用的是 PHP,您还可以使用strrchr它返回从字符串中字符的最后一次出现到结尾的所有内容。或者您可以使用strrposand的组合substr,首先找到最后一次出现的位置,然后从该位置获取子字符串直到末尾。或explode和array_pop,在 处拆分字符串/并获得最后一部分。
回答by James Socol
You can also get the "filename", or the last part, with the basenamefunction.
您还可以使用basename函数获取“文件名”或最后一部分。
<?php
$url = 'http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123';
echo basename($url); // "p1f3JYcCu_cb0i0JYuCu123"
On my box I could just pass the full URL. It's possible you might need to strip off http:/from the front.
在我的盒子上,我可以只传递完整的 URL。您可能需要http:/从前面剥离。
Basename and dirnameare great for moving through anything that looks like a unix filepath.
Basename 和dirname非常适合移动任何看起来像 unix 文件路径的东西。
回答by rasjani
/^.*\/(.*)$/
^= start of the row
^= 行的开始
.*\/= greedy match to last occurance to / from start of the row
.*\/= 贪婪匹配最后一次出现到/从行的开头
(.*)= group of everything that comes after the last occurance of /
(.*)= 最后一次出现 / 之后出现的所有事物的组
回答by ghostdog74
you can also normal string split
你也可以正常的字符串拆分
$str = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123";
$s = explode("/",$str);
print end($s);
回答by eyelidlessness
This pattern will not capture the last slash in $0, and it won't match anything if there's no characters after the last slash.
此模式不会捕获 中的最后一个斜杠$0,如果最后一个斜杠后没有字符,它将不匹配任何内容。
/(?<=\/)([^\/]+)$/
Edit: but it requires lookbehind, not supported by ECMAScript (Javascript, Actionscript), Ruby or a few other flavors. If you are using one of those flavors, you can use:
编辑:但它需要后视,ECMAScript(Javascript、Actionscript)、Ruby 或其他一些风格不支持。如果您使用其中一种口味,您可以使用:
/\/([^\/]+)$/
But it willcapture the last slash in $0.
但它会捕获$0.
回答by hughdbrown
Not a PHP programmer, but strrposseems a more promising place to start. Find the rightmost '/', and everything past that is what you are looking for. No regex used.
不是 PHP 程序员,但strrpos似乎是一个更有希望的起点。找到最右边的“/”,以及您正在寻找的所有过去。没有使用正则表达式。
Find position of last occurrence of a char in a string
查找字符串中最后一次出现字符的位置
回答by eapo
based on @Mark Rushakoff's answer the best solution for different cases:
基于@ Mark Rushakoff的回答,针对不同情况的最佳解决方案:
<?php
$path = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123?var1&var2#hash";
$vars =strrchr($path, "?"); // ?asd=qwe&stuff#hash
var_dump(preg_replace('/'. preg_quote($vars, '/') . '$/', '', basename($path))); // test.png
?>

