简体   繁体   中英

What's the regular expression for positive whole numbers only? (zero is not allowed)

What's the regular expression for positive whole numbers only? (zero is not allowed)

I was able to get the regular expression for numbers only which is ^\\d+$ . I've been trying to look for one online but it's already been an hour so I've decided to post it here on Stack Overflow.

matches:

1 || 2 || 444 || 9000 || 012

non-matches:

9.2 || -90

^0*[1-9]\\d*$将匹配除零之外的所有数字。

May be you are looking for ^[1-9]+$ pattern. It'll match 1245 , 2253 etc. But not 1003 . or 0124 . If you want to match the 3rd number ( 1003 ) as well use ^[1-9]\\d*$ pattern.

In php you'd use

preg_match('/^[1-9]\d*$/', $subject, $matches)

This is obviously:

[1-9]\d*

(one to nine followed by zero or more digits.) This would exclude things like 0190, of course.

I know you asked for a regex, but you might consider

$opts = array(
    'options' => array(
        'min_range' => 1,
        'max_range' => PHP_INT_MAX
    )
);
$n = filter_var($input, FILTER_VALIDATE_INT, $opts);
/^\+?([1-9]|0.)[0-9\.]*$/

this pattern may help you, but i think you must your programming language skills to do arithmetic check. for example in php . " $is_positive = $input > 0 " yes it is so easy :)

尝试这个

[1-9]\d*|[0]*[1-9]\d*

这是允许0到100而不是01的一个

^((([0]$)|[1-9][0-9]{0,1})|100)$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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