Viewed   65 times

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.

 Answers

5

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 );
Friday, September 2, 2022
5

The Arabic regex is:

[u0600-u06FF]

Actually, ?-? is a subset of this Arabic range, so I think you can remove them from the pattern.

So, in JS it will be

/^[a-z0-9+,()/'su0600-u06FF-]+$/i

See regex demo

Tuesday, October 11, 2022
1

You can match this:

.*/

and replace with your text.

DEMO

Sunday, November 6, 2022
 
4

You can try something like:

.[^.]*$

to match everything including and after the last dot. If you don't want to include the last dot, then you can use a positive lookbehind:

(?<=.)[^.]*$
Tuesday, August 30, 2022
3

For this PHP regex:

$str = preg_replace ( '{(.)1+}', '$1', $str );
$str = preg_replace ( '{[ '-_()]}', '', $str )

In Java:

str = str.replaceAll("(.)\1+", "$1");
str = str.replaceAll("[ '-_\(\)]", "");

I suggest you to provide your input and expected output then you will get better answers on how it can be done in PHP and/or Java.

Sunday, October 9, 2022
 
haodong
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :