PHP preg_match range

php, preg-match

Solution

Copied from comment above:

I suggest matching simply `([0-9]{1,2})` (maybe wrapped in `\b`, based on input format) and filtering the numeric value later, in PHP code.

See also Raymond Chen's thoughts on the subject.

Problem

I want to use preg_match to match numbers 1 - 21. How can I do this using preg_match? If the number is greater than 21 I don't want to match anything. ``` example preg_match('([0-9][0-1]{0,2})', 'Johnathan 21'); ```

Original source