0
I created a regular expression that does a JSON search for the name and value of the field, but it does not work correctly when the searched value contains parentheses, such as a DDD for example, so in this case search ignores the parentheses and brings all the results that have anywhere the searched value, (11) for example, and not only when this value is in parentheses, it is a DDD.
My regular expression:
$Search = sprintf('\'"%s":"([^"]*)%s([^"]*)"\'', 'telefone', '(11)');
Research:
Contatos::whereRaw("contains REGEXP {$Search}")->get();
Returned values:
(11) --954584
(45) -411081
(62) -581107
Updating: In this test I am using preg_match to search in a string that contains the phone field, realize that instead of the regular expression return only the DDD, is returning the number 5011 present 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);
Upshot:
array(4) {
[0]=>
string(28) ""telefone":"(011) 5011-3344""
[1]=>
string(7) "(011) 5"
[2]=>
string(3) "011"
[3]=>
string(5) "-3344"
}
What I need to change in this regular expression so that these parentheses are not ignored?
It would not be better to parse this JSON and iterate until you find a match?
– Sergio
No, I count with more than 10 thousand items of the database, it is unviable, besides that the search is not the only filter.
– Rafael Alexandre
You’re getting that data from the comic book? In that case, you can’t get that information from the comic book?
– Sergio
No, the platform is a lead manager application, these leads are received via JSON API in an extremely varied way, and has a listing to which the client manages, so this my question is related to a search filter.
– Rafael Alexandre
Okay. Put a piece of JSON in here so we have some material to test. You can even put an example that doesn’t work on -> https://regex101.com/ , save and put it back here to see it too.
– Sergio
Updated, see that the REGEX should be returning only the DDD (011), but is returning the value 011 present in the phone number, ignoring the parentheses.
– Rafael Alexandre
Raphael, in this example you have set what is the final result you want to obtain? It is not yet clear to me. Do you want to extract the indicative with or without parentheses from a given JSON? (in this case you can use something like this: https://regex101.com/r/fU1dV5/1) or you want to replace content within JSON?
– Sergio
Sergio, what I need is that when the string to be searched is in parentheses, these parentheses are not ignored and the result is exactly the searched string.
– Rafael Alexandre
Let’s go continue this discussion in chat.
– Sergio
In summary, what currently occurs is that when I use a string with parentheses, (11) for example, these parentheses are being ignored and the returned value is any one that has the integer value, such as "551199", instead of "(11) 9999999".
– Rafael Alexandre