Solved

Regular expression that only finds last instance

Posted on 2016-09-09
1
83 Views
Last Modified: 2016-09-09
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.
0
Comment
Question by:mock5c
[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
1 Comment
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 41791944
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.
/(\s|-)[\da-z]+-\d+$/

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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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