regex to get last part of string

With an example string like this
my-example-string-1

I want to get the 1 - it could be any number and the string could be anything, but the part I want will always be a dash followed by a number at the end

What would the regex be to grab the number?
LVL 58
GaryAsked:
Who is Participating?
 
Ray PaseurConnect With a Mentor Commented:
I'd skip the regex and go with explode('-', $str) then use end() and if you're not sure about the numbers, maybe test for is_numeric().  If you have some test cases I'd be glad to see if I can code up a sample.
0
 
GaryAuthor Commented:
So damn obvious - shouldn't try to write code when I'm drinking.
0
 
Dave BaldwinFixer of ProblemsCommented:
I'd use Ray's method also.  Actually I think I have used it.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
GaryAuthor Commented:
Actually I just looked again at my SQL and it does this already for me (gets the last number used) - not with it.
It's for ensuring unique URL's so I needed the last counter when not unique.
0
 
Ray PaseurCommented:
In Washington, DC, we have a Meetup Group called "Drunk Coders!"
0
 
GaryAuthor Commented:
Can I be an honourary member?
I do my best work inebriated.
0
 
GaryAuthor Commented:
0
 
Ray PaseurCommented:
Bingo!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.