What regular expression will match decimal values restricted to 6 decimal positions?
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.
The correct expression is
^((\+|-)?(\d*))+((\.|,)\d{0,6})?$
You can test it at http://www.fileformat.info/tool/regex.htm.
You can try with:
^[+-]?\d*([.,]\d{1,6})?$
(+|-)?\d*[.,]?\d{0,6}$
and 7 gnomes to be posted.
精彩评论