RegExp is not suitable for an array of patterns

I need to check "If not matching". Does the match work correctly:

<?php
   $string1 = 'BB123';
   $string2 = 'ZZ123';

   $pattern = '/^(AA.*|BB.*|CC.*)$/';

   echo preg_match($pattern, $string1);
   echo preg_match($pattern, $string2);

      

I get 1 and 0 and everything is fine. But if you change the template to

$pattern = '/^(?!AA.*|BB.*|CC.*)$/';

      

I get 00.

Help me get 0 and 1 by changing only the template.

+3


source to share


2 answers


    ^(?!AA.*|BB.*|CC.*).*$

      



Use this.If negative lookahead fails then accept string. *.

+2


source


you can try this

<?php
    $string1 = 'BB123';
   $string2 = 'ZZ123';

   $regexp = "(AA.*)|(BB.*)|(CC.*)"; 

   $pattern = "/^((?!(".$regexp.")).)*$/";

   echo preg_match($pattern, $string1);
   echo preg_match($pattern, $string2);
   ?>

      



Demo

0


source







All Articles