From this question How to match this using regex
Right now i want to search for 3D D keyword from user submitted data. The rule is as long as 3D and D is present in the sentence, it is valid (case insensitive).
For example:
3Dzzzzzzzzzzzzzzzzzzz (invalid because no second occurence of D)
zzzzzD (invalid because no 3D)
xxx3DzzzzzD (valid because got 3D and D in string)
I am using this regex now but somehow it has one problem.
$subject = 'BLASHSH*3D*8qw9e08e2323*D*';
if(preg_match('/(?=.*3D)(?=.*D).*/i', $subject))
{
echo 'pattern match';
}
else
{
echo 'fail';
}
The problem is this string also will return true
3D (it should be invalid because no second occurrence of D)
3D ABC (it should be invalid because no second occurrence of D)
How to solve this?
Assuming
3DD,D3D, andcccDzzz3Dare all valid:And since you used the
imodifier before I assume you want to accept3dandd? If not just remove theifrom the end of the regex.