PHP 正则表达式 - 有效的浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3941052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:31:12  来源:igfitidea点击:

PHP regex - valid float number

phpregex

提问by Chameron

I want user only input 0-9 and only once "."

我希望用户只输入 0-9 并且只输入一次“。”

 patt = /[^0-9(.{1})]/

 1.2222 -> true
 1.2.2  -> false (only once '.')

help me , thank !

帮帮我,谢谢!

回答by Core Xii

/^-?(?:\d+|\d*\.\d+)$/

This matches normal floats e.g. 3.14, shorthands for decimal part only e.g. .5and integers e.g. 9as well as negative numbers.

这匹配正常的浮点数,例如3.14,仅小数部分的简写.5和整数,例如9以及负数。

回答by user187291

this is what you're looking for

这就是你要找的

$re = "~        #delimiter
    ^           # start of input
    -?          # minus, optional
    [0-9]+      # at least one digit
    (           # begin group
        \.      # a dot
        [0-9]+  # at least one digit
    )           # end of group
    ?           # group is optional
    $           # end of input
~xD";

this only accepts "123" or "123.456", not ".123" or "14e+15". If you need these forms as well, try is_numeric

这仅接受“123”或“123.456”,而不接受“.123”或“14e+15”。如果您还需要这些表格,请尝试 is_numeric

回答by Gordon

Regular Expressions are for matching string patterns. If you are not explicitly after validating the input string's format (but the actual value), you can also use

正则表达式用于匹配字符串模式。如果您在验证输入字符串的格式(但实际值)后没有明确,您还可以使用

filter_var("1.33", FILTER_VALIDATE_FLOAT);

to make sure the input can be used as a float value. This will return FALSEif it is not a float and the float or integer value otherwise. Any type jugglingrules apply.

确保输入可以用作浮点值。FALSE如果它不是浮点数,则返回,否则返回浮点数或整数值。任何类型的杂耍规则都适用。

回答by Vantomex

This regex:

这个正则表达式:

\d*(?:\.\d+)?

will give results:

将给出结果:

123 -> true
123.345 -> true
123. -> true
.345 -> true
0.3345 -> true

However, you must check emptiness of the input before using it because the regex also permit zero-lengthinput.

但是,您必须在使用之前检查输入是否为空,因为正则表达式也允许零长度输入。

回答by Pekka

You can use is_numeric()with the caveat that it accepts a bit more than one usually wants (e.g. 1e4).

您可以使用is_numeric()一个警告,即它接受的比通常需要的多一点(例如1e4)。

回答by Hannes

Why not use http://php.net/manual/en/function.is-float.php? But anyhow, the RegEx would be ^[\d]+(|\.[\d]+)$have fun!

为什么不使用http://php.net/manual/en/function.is-float.php?但无论如何,RegEx 会很^[\d]+(|\.[\d]+)$有趣!

回答by AndiDog

Why not just use is_numericif you're not experienced with regular expressions.

is_numeric如果您对正则表达式没有经验,为什么不直接使用。

As to your regex: .matches all characters, \.matches a dot. {1}is not necessary. And I have no clue what you're trying to do with [^ ... ]. Read the regular expressions tutorialif you really want to use regular expressions somewhere in your code.

至于你的正则表达式:.匹配所有字符,\.匹配一个点。{1}没有必要。而且我不知道你想用[^ ... ]. 如果您真的想在代码中的某处使用正则表达式,请阅读正则表达式教程