Take the 2-minute tour ×

What is the regular expression to allow for numbers between -90.0 and +90.0? The numbers in between can be floating or whole numbers.

share|improve this question
Why a regular expression? Why not just use numerical comparison? –  Jason McCreary Nov 4 '10 at 2:41
Including or excluding 90? Do you need to support "e" notation? If so, that's going to be one ugly regex. –  dan04 Nov 4 '10 at 3:24
add comment

3 Answers

up vote 17 down vote accepted

I don't think you want to use a Regex for this. Use Double.Parse() (or Double.TryParse()) if your data is stored in a string, and then check the resulting value to ensure that it falls within the desired range. For example:

public bool IsInRange(string value)
   bool isInRange = false;

   double parsed = 0;
   if (Double.TryParse(value, out parsed))
      // use >= and <= if you want the range to be from -90.0 to 90.0 inclusive
      isInRange = value > -90.0 && value < 90.0;

   return isInRange;

If your value is already a double, then it's even easier -- no parsing required.

share|improve this answer
add comment

Not that you really want to use a Regex here (you should parse it, instead, and do the comparison on a numeric type - such as float, or double). But, you could do this:


This will match -90.0 to 90.0, inclusive. If you want it to be exclusive, drop the 90.0 clause.

  • negative (optional):

  • single digit
    OR double digit, 10-89
    PLUS decimal, 0-9 (optional):

  • OR 90
    PLUS decimal, 0 (optional):

If you want to support more decimal points, then change the 0-89.9 clause to:

  • Specific precision (seven, in this case) \d|([1-8][0-9])(\.\d{1,7})?
  • Infinite precision \d|([1-8][0-9])(\.\d+)?

Escape, if necessary

share|improve this answer
Could add as an option... ([-+]?([0-8]\.\d{1,7}|9\.0{1,7})[eE][+]?0{0,2}1) or is that going too far? –  user7116 Nov 4 '10 at 12:45
@sixlettervariables: No regex goes too far! I won't stop until the entire world is composed solely of regex ;) –  Merlyn Morgan-Graham Nov 4 '10 at 16:08
add comment


This is a problem that would be better solved with a check. But, if you want a regex, you can have a regex.


will work, I think. Match an optional '-', followed by any number of zeros, followed by either 90 with any number of zeros, or a number that consists of an optional tens digit between 1 and 8, followed by a ones digit, followed by a optional decimal and decimal places. But you see why using a regex for this is so messy. Check the bounds as a numbers, not a series of numerals.

share|improve this answer
That regex matches numbers in the range the OP specified, but it also matches 0123456789, 9876543210, 9099999999, -00000000000000000, and many other invalid strings. –  Alan Moore Nov 4 '10 at 15:34
Fixed it, I think. Now the numbers following the decimal place can only show up when the decimal place is there. –  Tyr Nov 4 '10 at 19:57
It still matches 900000000000000 –  gnarf Nov 4 '10 at 20:07
Oops, I missed that. You can see that I don't use regexes very often. –  Tyr Nov 4 '10 at 20:23
add comment

Your Answer


