regular expression help

Posted on 2006-11-09
Last Modified: 2010-04-16
I'm trying to make a regular expression to validate that a number entered is in a range from  99.99 to -99.99 where the decimal is optional but then would only take the last two digits.

examples of good values would be : -3.4 , 33 , .92 , etc...
Question by:Orlando15767
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
  • 4
  • 2
  • 2
  • +1
LVL 19

Expert Comment

ID: 17911428

How about

Optionally negative:

Any sequence of digits (possibly none)

Followed by a chunk of stuff to deal with the decimal place:

If we're gonna have a decimal component there must be a decimal point:

Followed by exactly two digits

And that concludes the decimal part, but let's make it optional:

The finished product:

LVL 19

Expert Comment

ID: 17911431
Having said that, in most languages there'll already be a function to determine if a string contains a numeric value, and another to round properly to two digits.  Perhaps that combination would work better for what you're doing?
LVL 84

Expert Comment

ID: 17911996
Independent Software Vendors: 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 84

Expert Comment

ID: 17912000
LVL 15

Accepted Solution

mish33 earned 125 total points
ID: 17912154
LVL 19

Expert Comment

ID: 17914035
Yeah, I completely missed the range requirement.  Still, the second half of mish33's expression seems unnecessary?  I don't know what class of numbers that's restricting.


should be sufficient.
LVL 15

Expert Comment

ID: 17917194
ha, what about .99?
LVL 19

Expert Comment

ID: 17917208
Heh, yeah, {0,2} would make more sense on that first one, huh.

Expert Comment

ID: 17917427
whats the language that you are using?

can you uses abs() then you dont have to worry about negatives. ..
simply do an if then and check if the value > 0 and < 100...

also... if this is for some form or report, you can check the properties on displaying the variable, 2:2... or something like that indicates number of digits and number of decimals...  

additionally, lots of languages have round or truncate functions.  you can define the length of digits using either function, but it obviously depends on what you are trying to display.


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

This is about my first experience with programming Arduino.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

632 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