Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

regular expression help

Posted on 2006-11-09
9
Medium Priority
?
242 Views
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...
0
Comment
Question by:Orlando15767
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17911428
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
 
LVL 19

Expert Comment

by:VoteyDisciple
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 85

Expert Comment

by:ozo
ID: 17911996
-?[1-9]?[0-9](\.[0-9][0-9])?
0
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 85

Expert Comment

by:ozo
ID: 17912000
-?[1-9]?[0-9]?(\.[0-9]?[0-9]?)?
0
 
LVL 15

Accepted Solution

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

Expert Comment

by:VoteyDisciple
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

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

Expert Comment

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

Expert Comment

by:mploschiavo
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
Loops Section Overview

885 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