Solved

regex to get last part of string

Posted on 2013-11-22
8
384 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IP 10.0.1.2 / 255.0.0.0 61 56
Extracting content from meta tag PHP MYHTML DOM 3 21
issue with DB import 1 20
MSSQL - Lock Row from reading by other programs 9 39
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

828 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