Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:15:12+00:00 2026-06-15T21:15:12+00:00

I think I created a working regular expression for what I need. Just wondering

  • 0

I think I created a working regular expression for what I need. Just wondering if anyone can break it or see a shorter way to write it.

The regular expression should validate the following…

  • Dollar sign optional
  • Negative numbers signified with parenthesis, not a minus
  • If negative, dollar sign should be outside the parenthesis
  • Commas are optional
  • Max number is 999999.99
  • Min number is (999999.99)
  • Decimals do not have to be supplied, but if so, no more than two
    digits

So here are some examples of valid ones…

9
$9
$0.99
($999,999.99)
(999999)
($999999)
(999,999)
99,999.9

This is what I have come up with:

^\$?(((\d{1,6}(\.\d{1,2})?)|(\d{1,3},\d{3}(\.\d{1,2})?)|\(((\d{1,6}(\.\d{1,2})?)|(\d{1,3},\d{3}(\.\d{1,2})?))\)))$

CORRECTION, my spec was wrong, if the dollar sign is used it must be INSIDE the parenthesis.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:15:13+00:00Added an answer on June 15, 2026 at 9:15 pm

    You can express “between one and six digits; comma before the last three digits is optional” a bit more tersely as \d{1,3}(,?\d{3})?. This also allows you to include only two copies of (\.\d{1,2})?: one for positive and one for negative, instead of one for positive-without-comma, one for positive-with-comma, etc.

    Also, \d{1,2} can be shortened slightly to \d\d?, though I’m not sure if that’s an improvement.

    So, barring some notation like (?(1)) to test if a backreference is set, here’s the shortest version I see:

    ^(\$?\d{1,3}(,?\d{3})?(\.\d\d?)?|\(\$?\d{1,3}(,?\d{3})?(\.\d\d?)?\))$
    

    One perhaps-undesirable aspect of your regex, and of this one, is that they will allow something like $00,012.7, even though no one uses leading zeroes that way. You can address that by requiring the first digit to be nonzero, and then adding a special case to handle $0 and (0.12) and so on:

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

    Edited to add: using a lookahead assertion like F.J suggests in his/her answer, the latter can be shortened to:

    ^(?!\(.*[^)]$|[^(].*\)$)\(?\$?(0|[1-9]\d{0,2}(,?\d{3})?)(\.\d\d?)?\)?$
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a PC based automation software package called Think'n'Do created by
I have a working Java app that can find the most recently created file
I think it would be easier to use function pointers if I created a
Guids are created using the new keyword which makes me think it's a reference
I think I saw someday a way to create a link without sending the
I think the following code can be used to create manipulators. #include<iostream> ostream &
I have a working perl script that grabs the data I need and displays
I'm working on a basic event form created from a model, but I keep
I'm currently working on a project where I've created a CA cert and a
I created application using android google map but its not working on my machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.