Solved

Regular expression that only finds last instance

Posted on 2016-09-09
1
96 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
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…
Suggested Courses

622 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