• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 133
  • Last Modified:

Regular expression that only finds last instance

If I have strings such as these:
1.  AB-123
2.  AB 123
3.  AB-123-1
4.  AB 123-1
5.  AB XYZ-1

I need a regular expression that determine if the end contains /\-\d+$/ and only #3, #4, and #5 to qualify.
The problem I'm running into is that the above regex matches for #1, #3, #4, and #5 since they all have hyphens.
If there's a nice regex that can be used to solve this, that would be great.  Otherwise, I'm open to javascript, such as using the split function.  I've been trying that unsuccessfully, too.

var splitter = str.split(/\-\d+$/)

I just don't have a good way to tell if the string qualifies with /\-\d+$/ when there are possible hyphen instances earlier in the string.
1 Solution
Kim WalkerWeb Programmer/TechnicianCommented:
Assuming that 1., 2., 3., 4., 5. at the beginning of each example and the spaces after them are not part of the search string but the new line at the end of each line is, the following should work for a test condition.

Open in new window

This searches for a space or dash followed by one or more alphanumeric characters followed by a dash and 1 or more numbers followed by a new line.

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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