-3

I am working on a window form which accepts certain range of value as input. So far, I could only find the range between 0 and 20000.

(20000|[0-9]|0[1-9]|[1-9]\d{0,3})$

Can somebody kindly help me with finding the range between 0.0 and 20479.0 (including decimals)?

Mistalis
  • 16,351
  • 13
  • 68
  • 91
Punk
  • 17
  • 4
  • 2
    What have you tried doing? (Also why do you need a regular expression for this?) – UnholySheep Jun 28 '17 at 08:08
  • 1
    do a simple comparison - a constant or configurable numeric value is easier to maintain than the obscure, specialized regex this would become: https://stackoverflow.com/a/22131040/1132334 – Cee McSharpface Jun 28 '17 at 08:19
  • Yes, regular expression are probably not your first choice here and overly complicating the problem – TheGeneral Jun 28 '17 at 08:21
  • @dlatikay mine input has some characters too in it like VB0.0-VB20479.0 – Punk Jun 28 '17 at 08:30
  • 1
    So the number can start with arbitrary characters? And - do you want the regex to match a range, i.e. "number hyphen number"? – SamWhan Jun 28 '17 at 08:42

5 Answers5

1

As comments suggest, regex is far from ideal in these cases. It can be done though, but get quite complex.

^(?:(?:1?\d{1,4}|20[0-3]\d\d|204[0-6]\d|2047[0-8])(?:\.\d+)?|20479(?:\.0+)?)$

This does it using two outer alternations - one to match the maximum number and optionally any number of zeroes as decimals. The other (first) has several sub-alternations matching the maximum for the different digits, and allowing an optional decimal point and decimals.

1?\d{1,4}     Matches 0-19999
20[0-3]\d\d   Matches 20000-20399
204[0-6]\d    Matches 20400-20469
2047[0-8]     Matches 20470-20478

See it here at regex101.

SamWhan
  • 8,038
  • 1
  • 14
  • 42
  • 1
    `1?\d{1,4}` matches even [`६` devanagari digit six](http://www.fileformat.info/info/unicode/char/096c/index.htm)... :-) – xanatos Jun 28 '17 at 08:39
  • @xanatos It appears that is true for regex in **python**, which isn't the case here. (And I petty you for all the comments you'll have to leave here on SO if you're to inform all post authors of that fact ;).) – SamWhan Jun 28 '17 at 10:24
  • It is true for .NET (and so for C#)... And my mission in life is to tell it one programmer at a time :-) – xanatos Jun 28 '17 at 10:40
  • @xanatos :O Thanks for the info. Did not know that. (That'll be really helpful to everyone mixing devanāgarī digits with arabic by mistake ;) ) – SamWhan Jun 28 '17 at 10:44
0

If you expect only integers (.0 at the end) you could try this

Mask is

^((1?[0-9]{0,4})|((20(([0-3][0-9]{0,2})|(4[0-7][0-9])))))$

If you need .0 at the end add \.0 before $. If you need double/decimal than precision/range would be required.

Pablo notPicasso
  • 2,923
  • 3
  • 15
  • 22
0

Here is a suggestion that allows numbers between 0 and 20479 with decimals:

^(0?[0-9]{0,4}(?:\.\d+)?|1\d{4}(?:\.\d+)?|20[0-4][0-7][0-8](?:\.\d+)?|20479(?:\.[0-7])?)$

As you can see, it is a bit complex, you may not want to do it with a regex.

Demo on regex101

Explanation

  • (0?[0-9]{0,4}(?:\.\d+)? between 0.0 and 9999.99 (decimals are optional)

  • 1\d{4}(?:\.\d+)? between 10000.0 and 19999.99 (decimals are optional)

  • 20[0-4][0-7][0-8](?:\.\d+)? between 20000.0 and 2048.99 (decimals are optional)

  • 20479(?:\.[0-7])? between 20479 and 20479.7


Update: Without decimals, you can use:

^(0?[0-9]{0,4}|1\d{4}|20[0-4][0-7][0-8]|20479$
Community
  • 1
  • 1
Mistalis
  • 16,351
  • 13
  • 68
  • 91
  • M really sorry but i wanted the range to be specifically from 0.0-0.7 till 20479.0-20479.7 which means i dont want the range exceeding above .7 at decimal place eg- 1.7-20479.7 – Punk Jun 28 '17 at 08:47
  • thanks a lot it worked well. And also you could help me without decimal of the same range. – Punk Jun 28 '17 at 09:56
  • @Punk Glad I helped. Could you *accept* the answer if it solved your issue? (the mark under the score at the left of the answer). I've also updated my answer to provide you another regex without decimals. – Mistalis Jun 28 '17 at 10:08
  • ohh m really sorry i forgot... upvoted !!! And also you could help me without decimal of same range please .. – Punk Jun 28 '17 at 10:13
  • Apart from have worse performance than the one in my answer ;) it also matches empty rows. Is that intentional? – SamWhan Jun 28 '17 at 10:28
0

Why would you use a regex that'll be hardly maintainable when you can use real code:

public bool IsValid(string input = "")
{
    double inputParsed;
    if (!double.TryParse(input, out inputParsed))
        return false;

    if(inputParsed < 0 || inputParsed > 20479)
        return false;

    return true;
}
Thomas Ayoub
  • 27,208
  • 15
  • 85
  • 130
0

Using regex get two numbers before and after the point.
Then check the numbers to hit the range.

var list = new List<string> { "VB0.0", "VB20479.0", "VB20479.7", "VB20480.0", "VB010000.0", "VB0.8", "VBx.y" };

string pattern = @"(\d+)\.(\d+)";

foreach (var input in list)
{
    var m = Regex.Match(input, pattern);

    if (m.Success)
    {
        string value1 = m.Groups[1].Value;
        string value2 = m.Groups[2].Value;

        bool result = value1.Length <= 5 && int.Parse(value1) <= 20479
                   && value2.Length <= 1 && int.Parse(value2) <= 7;

        Console.WriteLine(result);
    }
    else Console.WriteLine(false);
}
Alexander Petrov
  • 11,401
  • 2
  • 15
  • 45