Search json tags with REGEXP ignoring accents

Asked

Viewed 61 times

0

How can I ignore the accents with regexp? with LIKE was working, however I was having trouble with posts whose number of tags was large.

SELECT id, JSON_EXTRACT(quiz_json, '$.tags') from quiz_publico where aprovado = 1 AND JSON_EXTRACT(quiz_json, '$.tags') REGEXP 'Desenhos|Series'

Example the Series tag can exist with or without accent in the database and need to return of the 2 forms

1 answer

1


Unfortunately you can’t. REGEXP uses the byte of the character, its code, which differentiates accented and not accented characters, since each one has a different code.

I already had this problem, and I found this response from the OS in English: Mysql REGEXP query - Accent insensitive search , and there he quotes a link from the documentation of MySQL: Mysql REGEXP, that has this text:

Warning The REGEXP and RLIKE Operators work in byte-Wise fashion, so they are not multibyte safe and may Produce Unexpected Results with multibyte Character sets. In addition, These Operators compare characters by their byte values and accented characters may not compare the Equal Even if a Given collation Treats them as Equal.

In free translation:

REGEXP and RLIKE operators work "byte way", so no are safe for multibytes and can produce unexpected results with multibyte character sets. In addition, these operators compare characters by their byte values and accented characters cannot be compared as equal*("and" is not equal to "is" for example)*, even if a given group treat them as equal.

  • Apparently it worked, I put a function in php to replace accents with regular expression Rules, Function replacerAcentos($string){ Return preg_replace(array("/(á|à|ã|â|à|","/(Á|À|Ã|Â|Ä)/","/(é|è|ê|ë)/","/(É|È|Ê|Ë)/","/(í|ì|î|ï)/","/(Í|Ì|Î|Ï)/","(ó|ò|õ|ô|ö)/","/(Ó|Ò|Õ|Ô|Ö)/","/(ú|ù|û|ü)/","/(Ú|Ù|Û|Ü)/","/(ñ)/","/(Ñ)/"), 
 explode(" ","(á|à|ã|â|ä) (Á|À|Ã|Â|Ä) (é|è|ê|ë) (É|È|Ê|Ë) (í|ì|î|ï) (Í|Ì|Î|Ï) (ó|ò|õ|ô|ö) (Ó|Ò|Õ|Ô|Ö) (ú|ù|û|ü) (Ú|Ù|Û|Ü) (ñ) (Ñ)"), $string);
}

  • yes, if replace or include all variations to compare it will work

Browser other questions tagged

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