Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 391
  • Last Modified:

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?
0
Gary
Asked:
Gary
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now