Solved

regex to get last part of string

Posted on 2013-11-22
8
380 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 108

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 82

Expert Comment

by:Dave Baldwin
ID: 39670648
I'd use Ray's method also.  Actually I think I have used it.
0
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 108

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 108

Expert Comment

by:Ray Paseur
ID: 39670720
Bingo!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now