4

I have this regex:

/^[0-9]+$/i

Used in this code:

preg_match('/^[0-9]+$/i', $var);

I want the following to be true: $var = 1; $var = 50; $var = 333;

And the following to be false: $var = 0; $var = 01; $var = 'abc';

I think what I have works so far except for the "0" part..?

I went through this guide (http://www.phpf1.com/tutorial/php-regular-expression.html) but was unable to come up with a complete answer.

Any help would be appreciated, thanks!

1
  • 1
    Just as a PS: You don't need the i modifier, if you aren't using any letters ;) Commented Sep 21, 2011 at 16:31

4 Answers 4

16
/^[1-9][0-9]*$/

Means: A non-zero digit followed by an arbitrary number of digits (including zero).

Sign up to request clarification or add additional context in comments.

Comments

4

just check for a non zero digit first then any number of other digits

preg_match('/^[1-9][0-9]*$/', $var);

Comments

2

There are a couple of ways you can do this:

/^[1-9][0-9]*$/

or

/^(?!0)[0-9]+$/

Either one of these should be fine. The first one should be more or less self-explanatory, and the second one uses a zero-width negative lookahead assertion to make sure the first digit isn't 0.

Note that regex isn't designed for numeric parsing. There are other ways of doing it that will be faster and more flexible in terms of numeric format. For example:

if (is_numeric($val) && $val != 0)

...should pick up any nonzero number, including decimals and scientific notation.

4 Comments

Your last code doesn't match the regex (in many many ways) :) E.g. the regex doesn't match 0xABC or 0.034e-19 or 01.
@NikiC - That's a fair point, although the last code (since edited) would have blocked those because of the leading zero. However, the regex would have blocked 1.234, and the PHP I posted wouldn't. I intended the last part just as an acknowledgement that regex isn't for number parsing, so I've edited to make that more clear.
The $val != 0 checks for zero values, not for a leading zero. Furthermore the last thing you normally want is to allow people to enter a number in scientific notation, hex or oct. Especially if you need to explain to them afterwards why their "012" got a "10" instead of a "12".
@NikiC - The PHP code is just an example of numeric parsing, not meant to be equivalent to the regex.
1

You can solve this by forcing the first digit to be different from 0 with '/^[1-9][0-9]*$/'

With your list of exemples:

foreach (array('1', '50', '333', '0', '01', 'abc') as $var) {
  echo $var.': '.(preg_match('/^[1-9][0-9]*$/', $var) ? 'true' : 'false')."\n";
}

This script gives the following results:

$ php test.php
1: true
50: true
333: true
0: false
01: false
abc: false

1 Comment

You don't need the parentheses. They're not doing anything in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.