clarkk clarkk - 4 months ago 14
PHP Question

preg match end of word/line or not a digit

How to match end of line/word or not a digit

pattern



/\d{3}[\b\D]/


match



123
123-
123 1abc
123a


no match



1234

Answer

If you want to match exactly 3 digits that aren't surrounded by any other digits, use negative lookarounds.

/(?<!\d)\d{3}(?!\d)/
  ^           ^
lookbehind  lookahead

Lookarounds are explained at regular-expression.info.

DEMO