0

A column in my spreadsheet contains data like this:

5020203010101/FIS/CASH FUND/SBG091241212

I need to extract the last part of string after forwward slash (/) i.e; SBG091241212

I tried the following regular expression but it does not seem to work:

\/.*$

Any Idea?

7 Answers 7

3

Try this:

'/(?<=\/)[^\/]*$/'

The reason your current REGEXP is failing is because your .* directive matches slashes too, so it anchors to the first slash and gives you everything after it (FIS/CASH FUND/SBG091241212).

Sign up to request clarification or add additional context in comments.

2 Comments

this also doesn't work http://regexpal.com/?flags=g&regex=%28%3F%3C%3D\%2F%29[^\%2F]*%24&input=5020203010101%2FFIS%2FCASH%20FUND%2FSBG091241212
@php_nub_qq: It's because you use a tester for javascript regexes (that doesn't support lookbehind assertions). Try with regex101.com and you will see that it works well.
1

You need to specify a matching group using brackets in order to extract content.

preg_match("/\/([^\/]+)$/", "5020203010101/FIS/CASH FUND/SBG091241212", $matches);

echo $matches[1];

3 Comments

This is the best answer, other people used code other than regular expression to get correct results.
@asim-ishaq: Yes, however, keep in mind that the regex way can be slower than the use of build-in functions.
Perfectly good answer, though it's not essential to use sub-matches. A look-behind would also suffice.
1

You could do it like this without reg ex:

<?php

echo end(explode('/', '5020203010101/FIS/CASH FUND/SBG091241212'));

?>

Comments

1

this will do a positive lookbehind and match upto a value which does not contain a slash

like this

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

this will only highlight the match . i.e. the last part of the url

demo here : http://regex101.com/r/pF8pS2

Comments

1

Make use of substr() with strrpos() as a look behind.

echo substr($str,strrpos($str,'/')+1); //"prints" SBG091241212

Demo

3 Comments

TBH regex gurus don't need to know abt it though ;)
Thanks, but if you want to see real gurus, you can take a look at this post: stackoverflow.com/questions/17039670/…
I just lost myself ! :)
0

You can 'explode' the string:

$temp = explode('/',$input);
if (!empty($temp)){
    $myString = $temp[count($temp)-1];
}

Comments

0

You can also use:

$string = '5020203010101/FIS/CASH FUND/SBG091241212';
echo basename($string);

http://www.php.net/manual/en/function.basename.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.