普通EX pression验证十进制数普通、十进制数、EX、pression

2023-09-03 01:04:47 作者:爷不疼你谁疼你~

常规EX pression应符合以​​下标准。之前和之后的点的元素的数量可以是任意。只有1个点是允许的,负号被允许仅第一的位置。我不需要逗号。

The regular expression should match below criteria. The number of elements before and after the dot can be any. Only 1 dot is allowed and negative sign is allowed at first position only. I do not require comma.

例如:

1
-1
-1.
1.
1.2
-.2
-0.2
000.300

以上所有EX pressions应该产生真实的。

All above expressions should result true.

所以,如果我分手。

可选负号的首位。 在零个或多个数字点之前。 点是可选的。可发生的最大一次。它可以是纯整数也。 O或多个数字点后。

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

你可能需要的是这样的:

What you probably want is this:

^-?\d*\.?\d*

这将给你一个可能的负号( - ), 后跟任意数量的数字( \ D * ), 其次是可能的小数点( \ ), 其次是小数点后任意数量的尾部位数( \ D * )。

Which will give you a possible negative sign (-?), followed by any number of digits (\d*), followed by a possible decimal point (\.), followed by any number of trailing digits after the decimal point (\d*).

既然你只是想验证它是否是一个有效的float与否,@MarcinJuraszek有一个好点的,你可能不希望在这里使用正则表达式。

Since you just want to validate whether it's a valid float or not, @MarcinJuraszek has a good point, you may not want to be using regex here.