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

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.

Leave a Comment