Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP preg_match range

Tags:

php

preg-match

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');
like image 551
James Avatar asked Nov 25 '25 03:11

James


1 Answers

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.