Regular expression to collect everything after the last /

I am new to regular expressions and wonder how a phrase is one that collects everything after the last.

I am retrieving the id used by Google GData.

my sample line

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

If identifier: p1f3JYcCu_cb0i0JYuCu123

Oh and I use PHP.

+41
php regex
Jul 19 '09 at 18:45
source share
8 answers

This matches at least one of the (nothing slashes) followed by the end of the line:

 [^/]+$ 


Notes:

  • There is no parence, because he does not need any groups - the result goes to group 0 (the match itself).
  • Uses + (instead of * ), so if the last character is a slash, it does not match (and does not match an empty string).


But most likely, a quicker and simpler solution is to use the function of processing the list of strings built into the language, i.e. ListLast( Text , '/' ) or an equivalent function.

For PHP, the closest strrchr function, which works as follows:

 strrchr( Text , '/' ) 

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

 substr( strrchr( Text, '/' ), 1 ); 
+80
Jul 19 '09 at 19:17
source share
— -

Generally:

 /([^/]*)$ 

The data you want will match the match of the first group.




Change Since you are using PHP, you can also use strrchr , which returns everything from the last occurrence of a character in a string to the end. Or you can use a combination of strrpos and substr , first find the position of the last occurrence, and then the substring from that position to the end. Or explode and array_pop , split the string by / and get only the last part.

+17
Jul 19 '09 at 18:50
source share

You can also get the "file name" or the last part using the basename function.

 <?php $url = 'http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123'; echo basename($url); // "p1f3JYcCu_cb0i0JYuCu123" 

In my window, I can just pass the full URL. You may need to remove http:/ from the front.

Basename and dirname are great for navigating everything that looks like a path to a unix file.

+11
Jul 19 '09 at 21:09
source share
 /^.*\/(.*)$/ 

^ = start of line

.*\/ = greedy match for last event to / from start of line

(.*) = group of everything that comes after the last event /

+6
Jul 19 '09 at 18:57
source share

you can also normalize line splitting

 $str = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123"; $s = explode("/",$str); print end($s); 
+3
Jul 20 '09 at 1:05
source share

This pattern will not write the last slash to $0 , and it will not match anyone if there are no characters after the last slash.

 /(?<=\/)([^\/]+)$/ 

Edit: but this requires a lookbehind not supported by ECMAScript (Javascript, Actionscript), Ruby, or several other flavors . If you use one of these fragrances, you can use:

 /\/([^\/]+)$/ 

But it will write the last slash at $0 .

+2
Jul 19 '09 at 18:56
source share

Not a PHP programmer, but strrpos seems like a more promising place to start. Find the rightmost '/' and all that has passed is what you are looking for. No regex.

Find the position of the last char occurrence in a string

+1
Jul 19 '09 at 19:50
source share

based on @ Mark Rushakov meet the best solutions for different cases:

 <?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 ?> 
  • Regular expression to collect everything after the last /
  • How to get file name from full path using PHP?
0
Sep 01 '17 at 7:28
source share



All Articles