regular expression help

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...
LVL 1
Orlando15767Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mish33Connect With a Mentor Commented:
-?([0-9]{1,2}(\.[0-9]{,2})?)|([0-9]{,2}\.[0-9]{1,2})
0
 
VoteyDiscipleCommented:
Hmmmm....

How about

Optionally negative:
-?

Any sequence of digits (possibly none)
[0-9]*

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
[0-9][0-9]

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


The finished product:

-?[0-9]*(\.[0-9][0-9])?
0
 
VoteyDiscipleCommented:
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?
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ozoCommented:
-?[1-9]?[0-9](\.[0-9][0-9])?
0
 
ozoCommented:
-?[1-9]?[0-9]?(\.[0-9]?[0-9]?)?
0
 
VoteyDiscipleCommented:
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.

-?[0-9]{1,2}(\.[0-9]{,2})?

should be sufficient.
0
 
mish33Commented:
ha, what about .99?
0
 
VoteyDiscipleCommented:
Heh, yeah, {0,2} would make more sense on that first one, huh.
0
 
mploschiavoCommented:
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.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.