Reputation: 1927
I have a regular expression:
/^ETW([0-9C])([0-9])([0-9])([0-9])([0-9])([A-L])([0-9])([0-9])([0-9])/g
Which matches the following things:
All these conditions are fulfilling with my RegEx but problem is now another condition is added that the Character 12
can only be Equal to
Character 10
or +1 only
from Character 10
How can i achieve this ?? i have tried Conditional RegEx but its not working can anyone help me out with this ?? Do i have to handle it with pragmatically i am sorry i am really new to RegEx conditional handling any helping material will also be useful.
Upvotes: 1
Views: 315
Reputation: 10838
One way would be to split it into two separate match operations, using the result from the first one to create the second.
function isValidFormat($str)
{
$pt1 = substr($str,0,10);
$r1 = '#^ETW(\d|C)(\d)(\d)(\d)(\d)([A-L])(\d)#';
//If you're not going to use all the match groups, use
//$r1 = '#^ETW(\d|C)\d{4}[A-L](\d)#';
//And instead of $matches[7], use $matches[2]
if(preg_match($r1,$pt1,$matches)>0)
{
$pt2 = substr($str,10);
$pt10 = intval($matches[7]);
$pt12M = $pt10==9 ? '9' : '('.$pt10.'|'.($pt10+1).')$';
$r2 = '#\d'.$pt12M.'$#';
return preg_match($r2,$pt2)>0;
}
else return false;
}
$isValid = isValidFormat('ETW09876D929');
var_dump($isValid);
You visit this link and hit run/F9 to test
Upvotes: 2