I have to check if a variable (php, preg_match) is from 1988 and 2011 using regex; I know how to do it with normal if/else, but I'd like to use regex for this!
Sometimes, regular expressions are not the only answer:
if( preg_match('/^\d{4}$/', $input) && $input>=1988 && $input<=2011 ){
}
Wouldn't be that easy, as regex is meant to match character by character. You could use something like this (probably wouldn't be a good idea).
/(198[89]|199\d|200\d|201[01])/
Try this:
/^[12][90][8901][8901]\z/
Why do you want to do this using regex?
One solution could be something along the lines of (?:198[8-9]|199[0-9]|200[0-9]|201[0-1])
.
Use preg_replace_callback :
<?php
preg_replace_callback('%([0-9]{4})%', function($match) {
$number = $match[1];
if($number < 1988 || $number > 2011) return; /* Discard the match */
/* Return the replacement here */
}, $input);
This is in my opinion the most flexible solution.