Solved

regex to get last part of string

Posted on 2013-11-22
8
382 Views
Last Modified: 2013-11-22
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
Comment
Question by:Gary
  • 4
  • 3
8 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39670643
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
 
LVL 58

Author Closing Comment

by:Gary
ID: 39670646
So damn obvious - shouldn't try to write code when I'm drinking.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39670648
I'd use Ray's method also.  Actually I think I have used it.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 58

Author Comment

by:Gary
ID: 39670656
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39670660
In Washington, DC, we have a Meetup Group called "Drunk Coders!"
0
 
LVL 58

Author Comment

by:Gary
ID: 39670663
Can I be an honourary member?
I do my best work inebriated.
0
 
LVL 58

Author Comment

by:Gary
ID: 39670666
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39670720
Bingo!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question