Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex pattern for numeric values [closed]

Tags:

regex

I need an regular expression pattern to only accept positive whole numbers. It can also accept a single zero.

I do not want to accept decimals, negative number and numbers with leading zeros.

Any suggestions?

like image 623
Michael Kniskern Avatar asked Nov 14 '08 15:11

Michael Kniskern


People also ask

How do you represent a number in regex?

\d for single or multiple digit numbers It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range. [1-9][0-9] will match double digit number from 10 to 99.

Can regex be used for numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.

What is ?: In regex?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.


1 Answers

^(0|[1-9][0-9]*)$ 
like image 183
Federico A. Ramponi Avatar answered Oct 14 '22 06:10

Federico A. Ramponi