0

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

1
  • Is it certain that there will always be a number at the end of the string? If not, what do you want to happen - should the regex fail to match, or should it match everything after the last slash regardless? Commented Jul 1, 2010 at 7:47

2 Answers 2

2

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.

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

Comments

1

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)

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.