Reputation: 10412
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');
Upvotes: 3
Views: 7122
Reputation: 1
You could stablish an incremental filter like this example for TCP ports (altougth includes port number 0), like the previous answer:
preg_match('/^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/', '62000');
Upvotes: 0
Reputation:
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.
Upvotes: 3
Reputation: 197659
Literally:
preg_match('~ (1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21)$~', 'Johnathan 21');
But maybe this is more nifty:
preg_match('~ ([1-9]|1[0-9]|2[01])$~', 'Johnathan 21');
Upvotes: 3