Reputation: 3135
I have to write a regular expression that will match the following patterns, it should match decimal values, which can be in the formats shown below.
- +100.00
- -100.00
- .6777
- 0.45555
- the normal entry of the decimal values like 100 100.25 100.... and also the decimal points should be restricted 6 decimal positions.
This is the regular expression I have written so far:
return Regex.IsMatch(value, "^((\\+|-)(\\d*))+((\\.|,)\\d{0,5})?$");
Currently if the value is like +100
or -100
the reqular expression matches. If I enter a value like 100
, it is not accepted and if I start with the a decimal point like .899
then IsMatch
returns false.
Upvotes: 1
Views: 358
Reputation: 33139
The correct expression is
^((\+|-)?(\d*))+((\.|,)\d{0,6})?$
You can test it at http://www.fileformat.info/tool/regex.htm.
Upvotes: 2