tags:

views:

47

answers:

2

I'm trying to use PHP preg to extract the integer at the end of the string below, in the example its "4", but could be any number coming after "INBOX/", also the "[email protected]" is a variable so it could be any address

STRING I'M EXTRACTING FROM:

/m/[email protected]/folder/INBOX/4

/STRING

I've been going in circles with this, I guess I don't really understand how to do this and the regex examples I am finding in my searches just don't seem to address something like this, I would greatly appreciate any help..

ps. If anyone knows of a good regex software for building queries like this (for extraction) I would appreciate letting me know as I have spent countless hours lately with regex and still haven't found a software that seems to help in this.. thanks

+2  A: 

Use:

#/([^/]*)$#

preg_match('#/([^/]*)$#', $str, $matches);

We first check for a slash. Then the capturing group is zero or more non-slash characters. Then, the end of the string. $matches[1] holds the result.

Matthew Flaschen
Thanks that works
Rick
+1  A: 

Why not simply match \d+$ - this will match any trailing number.

if (preg_match('/\d+$/', $subject, $match)) {
    $result = $match[0];
} else {
    $result = "";
}

If you want to match anything (even it it's not a number) after the last slash, just use [^/]+$ instead:

preg_match('#[^/]+$#', $subject, $match)
Tim Pietzcker