Click here to Skip to main content
15,171,576 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hi All,

Can anyone help me on regular expression to allow numbers from -100.000 to +200.000.

Thanks in advance

Regards
Sherin
Posted
Comments
Hiren solanki 14-Dec-10 4:35am
   
Why don't you use Comparison operator, It's good for your solution rather then writing cumbersome regex to only achieve simple solution.
Kyrielia 14-Dec-10 4:47am
   
I agree with Hiren. All you'd need to do to is 'if((-100 < i) && (i < 200))' where i is your number.
E.F. Nijboer 14-Dec-10 5:17am
   
I agree with all above. Regex is for patterns and you are working on a range which adds unnecessary complexity when using regex for that.

1 solution

I agree with Hiren: you could use a Regex, but it would be a sledgehammer to crack a nut.
The trouble is that the regex would have to cope with:
200
199
7.888
-13.6
but reject
-100.001
Not easy to write in the first place, not easy to maintain (what if the limits change?).

It is not a good Regex task: if you must do this from a string than do it this way:
C#
bool ValidateFloat(string s)
    {
    float f;
    if (float.TryParse(s, out f))
        {
        if ((f >= -100.000) && (f <= 200.000))
            {
            return true;
            }
        }
    return false;
    }
If nothing else, it is a lot easier to read and understand!
   
Comments
Hiren solanki 14-Dec-10 7:58am
   
That's a good answer with good understanding of regex scope. Take a chilled 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900