Solved

# regular expression help

Posted on 2006-11-09
239 Views
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...
0
Question by:Orlando15767
[X]
###### 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
• 4
• 2
• 2
• +1

LVL 19

Expert Comment

ID: 17911428
Hmmmm....

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

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?
0

LVL 84

Expert Comment

ID: 17911996
-?[1-9]?[0-9](\.[0-9][0-9])?
0

LVL 84

Expert Comment

ID: 17912000
-?[1-9]?[0-9]?(\.[0-9]?[0-9]?)?
0

LVL 15

Accepted Solution

mish33 earned 125 total points
ID: 17912154
-?([0-9]{1,2}(\.[0-9]{,2})?)|([0-9]{,2}\.[0-9]{1,2})
0

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.

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

should be sufficient.
0

LVL 15

Expert Comment

ID: 17917194
0

LVL 19

Expert Comment

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

LVL 2

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.

0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may heâ€¦
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in aâ€¦
Six Sigma Control Plans
###### Suggested Courses
Course of the Month2 days, 9 hours left to enroll