Solved

regex to get last part of string

Posted on 2013-11-22
8
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 110

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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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 110

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 110

Expert Comment

by:Ray Paseur
ID: 39670720
Bingo!
0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

636 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