简体   繁体   English

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

[英]Regex to validate numbers 0-100 with up to two decimal places

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. 所以我知道只使用php is_numeric函数会更容易,但是我想做的是创建一个正则表达式,主要用于学习正则表达式,以测试测试成绩。 The range can be 0-100, but can have 2 decimal places. 范围可以是0-100,但可以有2个小数位。 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. 字符串的开头找到两个字符0-9。 optional end of string decimal and find characters 0-9, for two places. 字符串十进制的可选结尾,找到两个位置的字符0-9。 optional end of string find characters 0-9, for 1 place. 字符串的可选结尾查找字符0-9,用于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 (?:100 | \\ d {1,2})-非捕获组,100或0-99
  • (?:.\\d{1,2})? (?:。\\ 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> $ tests = array(0,5,10,50,100,99.5,75.43,75.436,101);

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

0 - 1 0-1

5 - 1 5-1

10 - 1 10-1

50 - 1 50-1

100 - 1 100-1

99.5 - 1 99.5-1

75.43 - 1 75.43-1

75.436 - 0 75.436-0

101 - 0 100-1

75F43 - 0 75F43-0

Yours doesn't work even when I add the slashes and remove the extra ) . 你当我添加斜线和删除多余的甚至不工作)

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

5 - 0 5-0

10 - 1 10-1

50 - 1 50-1

100 - 1 100-1

99.5 - 0 99.5-0

75.43 - 1 75.43-1

75.436 - 0 75.436-0

101 - 1 101-1

75F43 - 1 75F43-1

Surround your expression with / characters /字符括住您的表情

http://php.net/manual/en/regexp.reference.delimiters.php http://php.net/manual/zh/regexp.reference.delimiters.php

gpojd's answer is correct. gpojd的答案是正确的。 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. 您习惯于以if-else语句的方式思考,但正则表达式实际上并不是那样工作的。

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. 因此,可以是100,后跟一个可选的.00,或者:首先,是一个可选的非零数字,然后是一个必填数字(这些数字从0到99),然后是一个点,后跟两位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM