正则表达式验证数字 0-100,最多保留两位小数
所以我知道使用 php is_numeric 函数会更容易,但我想做的是创建一个正则表达式,主要用于学习正则表达式,以测试测试分数。范围可以是 0-100,但可以有 2 位小数。这是我到目前为止所拥有的,但它不起作用。
if (preg_match("^([0-9]){2,3})(.[0-9]{2})?$([0-9]{1})?$", $quiz1))
{
echo "match";
}
else {
$error = true;
}
如果我正确地理解字面意思:
字符串开头查找字符 0-9,用于两个位置。 可选的字符串小数结尾并查找字符 0-9(两个位置)。 可选字符串末尾查找字符 0-9,占 1 个位置。
So I know it would be easier to just use the php is_numeric function, but what i'm trying to do is create a regular expression, mostly for learning regex, to test test scores. The range can be 0-100, but can have 2 decimal places. This is what I have so far and its not working.
if (preg_match("^([0-9]){2,3})(.[0-9]{2})?$([0-9]{1})?$", $quiz1))
{
echo "match";
}
else {
$error = true;
}
If I'm thinking correctly the literal meaning:
start of string find characters 0-9, for two places.
optional end of string decimal and find characters 0-9, for two places.
optional end of string find characters 0-9, for 1 place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不做这样的事情呢?
结果:
即使我添加斜杠并删除额外的
,你的也不起作用)
。Why not something like this?
Results:
Yours doesn't work even when I add the slashes and remove the extra
)
.用
/
字符包围表达式http://php. net/manual/en/regexp.reference.delimiters.php
Surround your expression with
/
charactershttp://php.net/manual/en/regexp.reference.delimiters.php
gpojd的答案是正确的。但是,这就是您的正则表达式不起作用的原因。
首先,您需要将第一个 $ 放在 () 内。因为否则它将需要匹配字符串的结尾,然后再匹配字符串的结尾,这当然是不可能的。
其次,点字符需要变成\。因为点本身可以匹配任何字符,但您需要一个实际的点。最后,您需要分隔符,就像其他人建议的那样。以下是您的正则表达式匹配的内容:
第一位、两位或三位数字,必填。
然后,任意字符后跟两个数字(可选)。
然后,字符串结尾,强制。
然后是一位数字(可选)。
然后,再次强制字符串结尾。
由于您是一名程序员,因此可能很难习惯使用正则表达式的思维方式;您习惯于根据 if-else 语句进行思考,但正则表达式实际上并不是这样工作的。
同样,上面的选项已经足够好了,但如果我要为此编写一个正则表达式,我会输入:
所以,要么 100 后跟一个可选的 .00,要么:首先是一个可选的非零数字,然后是一个强制数字(这些使得数字 0 到 99),然后可以选择一个点,后跟两位数字。
gpojd's answer is correct. However, here's why your regex isn't working.
First of all, you want the first $ inside the ()'s. Because otherwise it will need to match the end of the string, and then later after that the end of the string again, which is of course impossible.
Second, the dot character needs to become \. because a dot by itself matches any character, but you want an actual dot. Lastly, you need delimiters, like someone else suggested. Here's what your regex matches:
first, two or three digits, mandatory.
Then, any character followed by two digits, optionally.
Then, the end of the string, mandatory.
Then, one digit, optionally.
Then, the end of the string again, mandatory.
Since you're a programmer, it can be hard to get used to the way of thinking with regular expressions; you're used to thinking in terms of if-else statements but regular expressions don't really work that way.
Again, the option above is good enough but if I were to write a regex for this I'd put:
So, either 100 followed by an optional .00, or: first, an optional nonzero digit and then a mandatory digit (those make the numbers 0 to 99), then optionally a dot followed by two digits.