REGEXP json search does not work properly when used string with parentheses

Asked

Viewed 367 times

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?

  • No, I count with more than 10 thousand items of the database, it is unviable, besides that the search is not the only filter.

  • You’re getting that data from the comic book? In that case, you can’t get that information from the comic book?

  • 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.

  • 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.

  • 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.

  • 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, 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.

  • 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".

Show 5 more comments

1 answer

1


Your regular expression passes 11 as a group, so the regular expression captures the parentheses you must escape them with a backslash:

$Search = sprintf('\'"%s":"([^"]*)%s([^"]*)"\'', 'telefone', '\(11\)');

In the second example you use (011) then it would be good to add an optional "0" with 0?. there are also groups in their expression that are not used ([^"]*) and these parentheses can be removed. Also, your expression delimiter (\') regular can cause confusion, I suggest using / or #. That way it stays:

$Search = sprintf('#"%s":"[^"]*%s[^"]*"#', 'telefone', '\(0?11\)');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.