Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 十进制值的正则表达式_C#_Regex - Fatal编程技术网

C# 十进制值的正则表达式

C# 十进制值的正则表达式,c#,regex,C#,Regex,我需要一个正则表达式,它允许.253或任何后跟点的数字 这是我的正则表达式,它允许0.253,但不允许.253 Regex match = new Regex(@"^[0-9]\d*(\.\d*)?$"); 解释 使用这个: Regex match = new Regex(@"^\d*(?:\.\d+)?$"); 删除[0-9]后,它将允许您使用.253。类似于^\d*\.?\d+$的问题是:为什么在这里使用正则表达式而不是框架解决方案decimal.TryParse?-请参见@Corak

我需要一个正则表达式,它允许.253或任何后跟点的数字

这是我的正则表达式,它允许0.253,但不允许.253

 Regex match = new Regex(@"^[0-9]\d*(\.\d*)?$");
解释

使用这个:

Regex match = new Regex(@"^\d*(?:\.\d+)?$");

删除[0-9]后,它将允许您使用.253。类似于^\d*\.?\d+$的问题是:为什么在这里使用正则表达式而不是框架解决方案decimal.TryParse?-请参见@Corak,但我无法限制小数点前后的数字。这就是原因。@m42谢谢你指出这一点,我在用手机,但我想它现在已经修好了。
^[0-9]+?\.?[0-9]+$

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character present in the list below «[0-9\.]*?»
   Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
   A character in the range between “0” and “9” «0-9»
   A . character «\.»
Match a single character in the range between “0” and “9” «[0-9]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at the end of a line (at the end of the string or before a line break character) «$»
Regex match = new Regex(@"^\d*(?:\.\d+)?$");