Regular expression where part of string must be number between 0-100

后端 未结 7 719
有刺的猬
有刺的猬 2020-12-02 02:23

I need to validate serial numbers. For this we use regular expressions in C#, and a certain product, part of the serial number is the \"seconds since midnight\". There are

相关标签:
7条回答
  • 2020-12-02 02:48

    Don't use regex? If you're struggling to come up with the regex to parse this that says that maybe it's too complex and you should find something simpler. I see absolutely no benefit to using regex here when a simple

    int value;
    if(!Int32.TryParse(s, out value)) {
        throw new ArgumentException();
    }
    if(value < 0 || value > 86400) {
        throw new ArgumentOutOfRangeException();
    }
    

    will work just fine. It's just so clear and easily maintainable.

    0 讨论(0)
提交回复
热议问题