十进制值的正则表达式

时间:2014-04-17 06:40:18

标签: c# regex

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

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

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

2 个答案:

答案 0 :(得分:0)

Regex regexObj = new Regex("^[0-9]+?\.?[0-9]+$");

<强>说明

^[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) «$»

答案 1 :(得分:0)

使用这个:

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