I am trying to compose a regular expression to match a numeric value expressed as a decimal multiple of .25 (ex. 1.25, 14.75).
// Must Match
1.0
1.25
1.250000
1.5
1.500
1.75
1.7500
// Must Not Match
1.2
1.46
1.501
1.99
So far I have the following expression: \d+(\.((0+)|(250*)|(50*)|(750*))). It works when I use online tooling like gskinner.com/regexr. When I use the expression in a validation attribute to seed my EntityFramework db, it produces validation errors:
[RegularExpression(@"^\d+(\.((0+)|(250*)|(50*)|(750*)))$", ErrorMessage = "Hours must be 15 minute increments expressed as decimals (ex. .0, .25, .5, .75)")]
public double Hours { get; set; }
Similar question (I am looking for a way to round the decimal portion of numbers up or down to the nearest .25, .5, .75, or whole number) but I need to use a regular expression to use the above data annotation.
Question:
-
Anyone see what’s wrong with my expression?
-
Bonus points if you can extend it to support whole numbers (ex.
4or4.25but not4.or4.62)
To match such number use regex pattern
To validate input to be such number use regex pattern
In both cases, the very first part
(?!0\d)is optional to disallow match/validate numbers with invalid leading zeros, such as000003.250, when match would trim them and take just3.250; validation would fail if this optional part is present in the regex.