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?

asim-ishaq
  • 2,120
  • 5
  • 27
  • 52

7 Answers7

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

Mitya
  • 30,438
  • 7
  • 46
  • 79
  • 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 Mar 21 '14 at 10:53
  • 2
    @php_nub_qq: It's because you use a tester for javascript regexes (that doesn't support lookbehind assertions). Try with http://regex101.com and you will see that it works well. – Casimir et Hippolyte Mar 21 '14 at 11:01
1

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

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

Demo

Shankar Damodaran
  • 65,155
  • 42
  • 87
  • 120
1

You could do it like this without reg ex:

<?php

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

?>
Latheesan
  • 19,790
  • 25
  • 89
  • 173
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];
Jon
  • 11,360
  • 4
  • 28
  • 44
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

aelor
  • 9,803
  • 2
  • 27
  • 43
0

You can 'explode' the string:

$temp = explode('/',$input);
if (!empty($temp)){
    $myString = $temp[count($temp)-1];
}
Gwenc37
  • 2,038
  • 7
  • 16
  • 22
0

You can also use:

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

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

mayrop
  • 611
  • 8
  • 15