Regular expression to match non-negative integers in PHP?

Posted by kavoir.com on Stack Overflow See other posts from Stack Overflow or by kavoir.com
Published on 2010-04-19T03:24:05Z Indexed on 2010/04/19 3:33 UTC
Read the original article Hit count: 165

Filed under:
|

I seem to get it to work with the following regexp by preg_match():

@^(?:[1-9][0-9]*)|0$@

Yet it's weird that it matches '-0', considering there are no '-' allowed at all in the regexp. Why?

What's more weird is that if you switch the parts divided by |:

@^0|(?:[1-9][0-9]*)$@

It matches all negative integers such as '-2' and '-10', etc.

What am I missing here? Any better regexp for non-negative integer?

© Stack Overflow or respective owner

Related posts about php

Related posts about regexp