Skip to main content
 首页 » 编程设计

c#之什么正则表达式将匹配限制为 6 个小数位的小数值

2024年02月23日30lexus

我必须编写一个匹配以下模式的正则表达式,它应该匹配十进制值,其格式如下所示。

  1. +100.00
  2. -100.00
  3. .6777
  4. 0.45555
  5. the normal entry of the decimal values like 100 100.25 100.... and also the decimal points should be restricted 6 decimal positions.

这是我到目前为止写的正则表达式:

return Regex.IsMatch(value, "^((\\+|-)(\\d*))+((\\.|,)\\d{0,5})?$"); 

目前,如果值类似于 +100 或 -100,则正则表达式匹配。如果我输入像 100 这样的值,它不会被接受,如果我以 .899 这样的小数点开头,则 IsMatch 返回 false。

请您参考如下方法:

正确的表达方式是

^((\+|-)?(\d*))+((\.|,)\d{0,6})?$ 

您可以在 http://www.fileformat.info/tool/regex.htm 进行测试.