Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match exactly 3 digits in PHP's preg_match? [duplicate]

Lets say you have the following values:

123
1234
4567
12
1

I'm trying to right a preg_match which will only return true for '123' thus only matching if 3 digits. This is what I have, but it is also matching 1234 and 4567. I may have something after it too.

preg_match('/[0-9]{3}/',$number);
like image 807
David Avatar asked Nov 25 '12 21:11

David


3 Answers

You need to anchor the regex

/^\d{3}$/
like image 105
Andy Lester Avatar answered Nov 08 '22 03:11

Andy Lester


What you need is anchors:

preg_match('/^[0-9]{3}$/',$number);

They signify the start and end of the string. The reason you need them is that generally regex matching tries to find any matching substring in the subject.

As rambo coder pointed out, the $ can also match before the last character in a string, if that last character is a new line. To changes this behavior (so that 456\n does not result in a match), use the D modifier:

preg_match('/^[0-9]{3}$/D',$number);

Alternatively, use \z which always matches the very end of the string, regardless of modifiers (thanks to Ωmega):

preg_match('/^[0-9]{3}\z/',$number);

You said "I may have something after it, too". If that means your string should start with exactly three digits, but there can be anything afterwards (as long as it's not another digit), you should use a negative lookahead:

preg_match('/^[0-9]{3}(?![0-9])/',$number);

Now it would match 123abc, too. The same can be applied to the beginning of the regex (if abc123def should give a match) using a negative lookbehind:

preg_match('/(?<![0-9])[0-9]{3}(?![0-9])/',$number);

Further reading about lookaround assertions.

like image 39
Martin Ender Avatar answered Nov 08 '22 03:11

Martin Ender


If you are searching for 3-digit number anywhere in text, then use regex pattern

/(?<!\d)\d{3}(?!\d)/

However if you check the input to be just 3-digit number and nothing else, then go with

/\A\d{3}\z/
like image 4
Ωmega Avatar answered Nov 08 '22 04:11

Ωmega