Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression to collect everything after the last /

Tags:

regex

php

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.

like image 718
Hellonearthis Avatar asked Jul 19 '09 18:07

Hellonearthis


People also ask

How do I get everything in regular expressions?

Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.

How do you match anything up until this sequence of characters in regular expression?

If you add a * after it – /^[^abc]*/ – the regular expression will continue to add each subsequent character to the result, until it meets either an a , or b , or c . For example, with the source string "qwerty qwerty whatever abc hello" , the expression will match up to "qwerty qwerty wh" .

How do you repeat a regular expression?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What does I mean after regex?

/i stands for ignore case in the given string. Usually referred to as case-insensitive as pointed out in the comment.


2 Answers

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 ); 
like image 193
Peter Boughton Avatar answered Sep 19 '22 02:09

Peter Boughton


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.

like image 35
Gumbo Avatar answered Sep 21 '22 02:09

Gumbo