I created a regular expression that does a search in JSON for the name and value of the field, but does not work correctly when the fetched value contains parentheses, like a DDD for example, so in this case the search ignores the parentheses and brings all results that have the searched value anywhere, (11) for example, and not only when this value is in parentheses, being a DDD.
My regular expression:
$Search = sprintf('\'"%s":"([^"]*)%s([^"]*)"\'', 'telefone', '(11)');
Search:
Contatos::whereRaw("contains REGEXP {$Search}")->get();
Values returned:
( 11 ) --954584
(45) --4 11 081
(62) - 53 11 07
Update: In this test I am using preg_match to search a string containing the phone field, realize that instead of the regular expression returning only the DDD, it is returning the number 5 011 in the phone number, ignoring the parentheses.
$DDD = '(011)';
$json = '{"name":"Teste JSON","email":"[email protected]","telefone":"(011) 5011-3344"}';
preg_match(sprintf('\'"telefone":"([^"]*)%s([^"]*)"\'', $DDD), $json, $matches);
var_dump($matches);
Result:
array(4) {
[0]=>
string(28) ""telefone":"(011) 5011-3344""
[1]=>
string(7) "(011) 5"
[2]=>
string(3) "011"
[3]=>
string(5) "-3344"
}
What do I need to change in this regular expression so that these parentheses are not ignored?