title: Regular expression to match non-negative integers in PHP?
body: <p>I seem to get it to work with the following regexp by <a href="https://www.php.net/manual/en/function.preg-match.php" rel="nofollow noreferrer"><code>preg_match()</code></a>:</p>
  
  <pre><code>@^(?:[1-9][0-9]*)|0$@
  </code></pre>
  
  <p>Yet it's weird that it matches <code>-0</code>, considering there are no <code>-</code> allowed at all in the regexp. Why?</p>
  
  <p>What's more weird is that if you switch the parts divided by <code>|</code>:</p>
  
  <pre><code>@^0|(?:[1-9][0-9]*)$@
  </code></pre>
  
  <p>It matches all negative integers such as <code>-2</code> and <code>-10</code>, etc.</p>
  
  <p>What am I missing here? Any better regexp for non-negative integer?</p>