Forum Moderators: coopster

Message Too Old, No Replies

Regular expression

How can i get just the last number in this list with a reg.exp?

         

asantos

4:04 pm on May 24, 2006 (gmt 0)

10+ Year Member



If I have
[3][4][1][41][2] << i must get: 2
[3][4][1][41][274] << i must get: 274

How can i achieve that with a regular expression?

eelixduppy

4:17 pm on May 24, 2006 (gmt 0)



Personally i wouldn't use regular expressions for this. I would do something like this:

$last_open = strrpos($string,"[");
$last_close = strrpos($string,"]");
$length = ($last_close-$last_open-1);
$last_num = substr($string,$last_open+1,$length);

[edited by: eelixduppy at 4:38 pm (utc) on May 24, 2006]

asantos

4:26 pm on May 24, 2006 (gmt 0)

10+ Year Member



Thanks eelixduppy!
Only one thing, the strrpos() takes the string as first argument and the char as the second one.

eelixduppy

4:29 pm on May 24, 2006 (gmt 0)



Ooops...i guess that's what happens when you are doing 100 things at once :)