phpregexdoctrinedoctrine-odm

Regex doesn't match with parentheses


I am using Doctrine MongoDB and I have the following code in the query builder:

$regexSeller = new Regex('^'.$search['seller'], 'i');
$raw->addOr($builder->matchExpr()->field('seller')->equals($regexSeller));

It works and find the good results with a search like this "Bob" but doesn't work when there are parentheses like "Bob (from Chicago)".

I guess the problem is in the first line but can't find a way to match with parentheses too.


Solution

  • You have to escape the special characters of your query. E.g. replace ( with \( and ) with \).

    In PHP you can use preg_quote(https://www.php.net/manual/en/function.preg-quote.php), like this:

    $regexSeller = new Regex('^'.preg_quote($search['seller']), 'i');
    

    Let me know if it worked! ;)

    Thanks!