Regular Expression to collect everything after the last /

PhpRegex

Php Problem Overview


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.

my example string is

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

Where the ID is: p1f3JYcCu_cb0i0JYuCu123

Oh and I'm using PHP.

Php Solutions


Solution 1 - Php

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).


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.

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

strrchr( Text , '/' )

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

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

Solution 2 - Php

Generally:

/([^/]*)$

The data you want would then be the match of the first group.


Edit   Since you’re using PHP, you could also use strrchr that’s returning everything from the last occurence of a character in a string up to the end. Or you could use a combination of strrpos and substr, first find the position of the last occurence and then get the substring from that position up to the end. Or explode and array_pop, split the string at the / and get just the last part.

Solution 3 - Php

You can also get the "filename", or the last part, with the basename function.

<?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.

Basename and dirname are great for moving through anything that looks like a unix filepath.

Solution 4 - Php

/^.*\/(.*)$/

^ = 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 /

Solution 5 - Php

you can also normal string split

$str = "http://spreadsheets.google.com/feeds/spreadsheets/p1f3JYcCu_cb0i0JYuCu123";
$s = explode("/",$str);
print end($s);

Solution 6 - Php

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.

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

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:

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

But it will capture the last slash in $0.

Solution 7 - Php

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

Find position of last occurrence of a char in a string

Solution 8 - Php

based on @Mark Rushakoff's answer the best solution 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
?>
  1. https://stackoverflow.com/questions/1150559/regular-expression-to-collect-everything-after-the-last
  2. https://stackoverflow.com/questions/1418193/how-to-get-file-name-from-full-path-with-php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHellonearthisView Question on Stackoverflow
Solution 1 - PhpPeter BoughtonView Answer on Stackoverflow
Solution 2 - PhpGumboView Answer on Stackoverflow
Solution 3 - PhpJames SocolView Answer on Stackoverflow
Solution 4 - PhprasjaniView Answer on Stackoverflow
Solution 5 - Phpghostdog74View Answer on Stackoverflow
Solution 6 - PhpeyelidlessnessView Answer on Stackoverflow
Solution 7 - PhphughdbrownView Answer on Stackoverflow
Solution 8 - PhpeapoView Answer on Stackoverflow