Search code examples
phpregexpreg-matchpreg-match-allpreg-grep

Regex for getting strings that contains only the words from the pattern list?


Consider the following array elements

 1.benclinton
 2.clintonharry
 3.harryben
 4.benwill
 5.jasonsmith
 6.smithclinton

Assume the pattern list is ben,harry,clinton, then the result I should get is

1.benclinton  
2.clintonharry  
3.harryben

So,essentially the result should contain strings that contains only the words that are in the pattern list. Order is not important

Also, each strings will not be having more than two words. i.e. bensmithwill will never be a case.

Since all my strings are in an array, I thought of using preg_grep in php to do this but i am struck in framing correct regex for this.

what regex can achieve this? Is there any other efficient way apart from regex matching that will do the work?

Thanks in advance!


Solution

  • Something like this

    $names_list = ['benclinton','clintonharry','harryben','benwill','jasonsmith','smithclinton'];
    $names = ['ben','harry','clinton'];  
    
    $matches = preg_grep('/('.implode('|',$names).')(?1)/', $names_list);
    //-  /(ben|harry|clinton)(?1)/  -- (?1) = recurse capture group 1 
    
    print_r($matches);
    

    Output

    Array
    (
        [0] => benclinton
        [1] => clintonharry
        [2] => harryben
    )
    

    Sandbox

    This requires that at least two of the names (even the same one 2x) match. But that is kind of a given in this case or everything would match.

    If you want to be extra careful, if the $names can contain something important to regex, such as +,*, \ etc. you can add this

    $matches = preg_grep('/('.implode('|',array_map(function($name){return preg_quote($name,'/');},$names)).')(?1)/', $names_list);