Solved

How can I change this regular expression

Posted on 2014-07-24
8
208 Views
Last Modified: 2014-07-25
I have the following regular expression that works great for me when it comes to currency numbers.  However I need the same format ability, except I also need to be able to except (negative numbers as well ie  ($245.00) .  How can this expression be changed to except negatives dollars for some people have credits.  Here is the expression.

"^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$"


thanks
0
Comment
Question by:mgmhicks
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40218063
If you mean your expression should match this:
($-245.00)
Then you should add the - in an alternation:
^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?-?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40218074
Hi mgmhicks;

The regex already excepts the following $245.00 and -$245.00 and $-245.00 patterns.
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 40218083
@Fernando: not if they are inside parenthesis: ($-254.00)
It's the 3rd alternation
0
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!

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40218095
@Dan you are correct. I took the ( ... ) to mean what was inside and not as part of the value.
0
 

Author Comment

by:mgmhicks
ID: 40218371
Sorry guys the number actually looks like this.   ($237.75)

That's what has to be ok, no need for the -  

Not sure how to change it my existing regular expression.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 40218476
This seems to work:

"^\(?\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)?$|^\(?-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$|^\(?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)?$"

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40218480
Hi mgmhicks;

I tested your Regex pattern in your post and it works for me just as is. Is there a specific value or range of values that it is not working with for you?
0
 

Author Closing Comment

by:mgmhicks
ID: 40219372
Thanks to both for the responses. Either worked.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

685 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