正则表达式验证数字 0-100,最多保留两位小数

发布于 2024-12-10 11:25:24 字数 390 浏览 1 评论 0原文

所以我知道使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-12-17 11:25:24

为什么不做这样的事情呢?

 /^(?:100|\d{1,2})(?:\.\d{1,2})?$/
  • ^ - 字符串开头
  • (?:100|\d{1,2}) - 非捕获组,100 或 0-99
  • (?:.\d{1,2})? - 可选的非捕获组(.# 或 .##)
  • $ - 字符串结尾

结果:

php > $tests = 数组(0, 5, 10, 50, 100, 99.5, 75.43, 75.436, 101);

php > foreach ($tests as $test) { 打印 $test . “-”。 preg_match("/^(?:100|\d{1,2})(?:\.\d{1,2})?$/", $test) 。 “\n”; }

0 - 1

5 - 1

10 - 1

50 - 1

100 - 1

99.5 - 1

75.43 - 1

75.436 - 0

101 - 0

75F43 - 0

即使我添加斜杠并删除额外的 ,你的也不起作用)

php > foreach ($tests as $test) { 打印 $test . “-”。
preg_match("/^([0-9]{2,3})(.[0-9]{2})?$([0-9]{1})?$/", $test) 。 “\n”; }
0 - 0

5 - 0

10 - 1

50 - 1

100 - 1

99.5 - 0

75.43 - 1

75.436 - 0

101 - 1

75F43 - 1

Why not something like this?

 /^(?:100|\d{1,2})(?:\.\d{1,2})?$/
  • ^ - beginning of string
  • (?:100|\d{1,2}) - non-capturing group, 100 or 0-99
  • (?:.\d{1,2})? - optional non-capturing group (.# or .##)
  • $ - end of string

Results:

php > $tests = array(0, 5, 10, 50, 100, 99.5, 75.43, 75.436, 101);

php > foreach ($tests as $test) { print $test . " - " . preg_match("/^(?:100|\d{1,2})(?:\.\d{1,2})?$/", $test) . "\n"; }

0 - 1

5 - 1

10 - 1

50 - 1

100 - 1

99.5 - 1

75.43 - 1

75.436 - 0

101 - 0

75F43 - 0

Yours doesn't work even when I add the slashes and remove the extra ).

php > foreach ($tests as $test) { print $test . " - " .
preg_match("/^([0-9]{2,3})(.[0-9]{2})?$([0-9]{1})?$/", $test) . "\n"; }
0 - 0

5 - 0

10 - 1

50 - 1

100 - 1

99.5 - 0

75.43 - 1

75.436 - 0

101 - 1

75F43 - 1

终难遇 2024-12-17 11:25:24

gpojd的答案是正确的。但是,这就是您的正则表达式不起作用的原因。

首先,您需要将第一个 $ 放在 () 内。因为否则它将需要匹配字符串的结尾,然后再匹配字符串的结尾,这当然是不可能的。

其次,点字符需要变成\。因为点本身可以​​匹配任何字符,但您需要一个实际的点。最后,您需要分隔符,就像其他人建议的那样。以下是您的正则表达式匹配的内容:

第一位、两位或三位数字,必填。
然后,任意字符后跟两个数字(可选)。
然后,字符串结尾,强制。
然后是一位数字(可选)。
然后,再次强制字符串结尾。

由于您是一名程序员,因此可能很难习惯使用正则表达式的思维方式;您习惯于根据 if-else 语句进行思考,但正则表达式实际上并不是这样工作的。

同样,上面的选项已经足够好了,但如果我要为此编写一个正则表达式,我会输入:

/^100(.00)?|([1-9]?[0-9])(\.[0-9]{2})?$/

所以,要么 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:

/^100(.00)?|([1-9]?[0-9])(\.[0-9]{2})?$/

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文