Solved

Custom Regular Expression

Posted on 2006-11-09
6
197 Views
Last Modified: 2012-05-05
First of all, is that an oxymoron?

I need a regular expression that is designed to handle a customer's account #. I know the format of the account number but it can change (dashes, spaces, last digit optional) so I'm not sure how to write this expression. I figure somebody who knows this stuff well could write it without too much trouble or at least point me in the right direction. Thanks!

The # is printed on the bill like this:   00-0000000 0    so 2 digits, dash, 7 digits, space, last digit

In the field where they enter their customer #, they may or may not enter the dash after the first two digits and may or may not enter the space before the last digit. I also want to make the last single digit optional all together. Please let me know if I did not explain that well enough. Thanks in advance.
0
Comment
Question by:megel6805
  • 4
6 Comments
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17909566
Try this:

\d{2}-?\d{7} ?\d{0,1}
0
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 17909575
Sorry - forgot beginning and end:

^\d{2}-?\d{7} ?\d{0,1}$
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17911062
Quantifiers provide a simple way to specify within a pattern how many times a particular character or set of characters is allowed to repeat itself.

*, which describes "0 or more occurrences",
+, which describes "1 or more occurrences", and
?, which describes "0 or 1 occurrence".

To learn to create regular expressions ref. this :
http://msdn2.microsoft.com/en-us/library/ms972966.aspx

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:muzzy2003
ID: 17912324
In this case, I have used the quantifiers {n} for exactly n and {n,m} for between n and m. The last one could obviously be written \d? or \d{0,1}.
0
 

Author Comment

by:megel6805
ID: 17934828
thanks!
0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 17936235
No problem.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

17 Experts available now in Live!

Get 1:1 Help Now