• <bdo id='wZLCw'></bdo><ul id='wZLCw'></ul>
    1. <small id='wZLCw'></small><noframes id='wZLCw'>

      <i id='wZLCw'><tr id='wZLCw'><dt id='wZLCw'><q id='wZLCw'><span id='wZLCw'><b id='wZLCw'><form id='wZLCw'><ins id='wZLCw'></ins><ul id='wZLCw'></ul><sub id='wZLCw'></sub></form><legend id='wZLCw'></legend><bdo id='wZLCw'><pre id='wZLCw'><center id='wZLCw'></center></pre></bdo></b><th id='wZLCw'></th></span></q></dt></tr></i><div id='wZLCw'><tfoot id='wZLCw'></tfoot><dl id='wZLCw'><fieldset id='wZLCw'></fieldset></dl></div>
      <legend id='wZLCw'><style id='wZLCw'><dir id='wZLCw'><q id='wZLCw'></q></dir></style></legend>

        <tfoot id='wZLCw'></tfoot>
      1. 如何在 MySQL 中进行正则表达式替换?

        时间:2023-08-19

          <legend id='kRiJ9'><style id='kRiJ9'><dir id='kRiJ9'><q id='kRiJ9'></q></dir></style></legend>

            • <small id='kRiJ9'></small><noframes id='kRiJ9'>

              <i id='kRiJ9'><tr id='kRiJ9'><dt id='kRiJ9'><q id='kRiJ9'><span id='kRiJ9'><b id='kRiJ9'><form id='kRiJ9'><ins id='kRiJ9'></ins><ul id='kRiJ9'></ul><sub id='kRiJ9'></sub></form><legend id='kRiJ9'></legend><bdo id='kRiJ9'><pre id='kRiJ9'><center id='kRiJ9'></center></pre></bdo></b><th id='kRiJ9'></th></span></q></dt></tr></i><div id='kRiJ9'><tfoot id='kRiJ9'></tfoot><dl id='kRiJ9'><fieldset id='kRiJ9'></fieldset></dl></div>
                  <tbody id='kRiJ9'></tbody>
              1. <tfoot id='kRiJ9'></tfoot>

                  <bdo id='kRiJ9'></bdo><ul id='kRiJ9'></ul>

                  本文介绍了如何在 MySQL 中进行正则表达式替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个大约有 50 万行的表格;varchar(255) UTF8 列 filename 包含文件名;

                  I have a table with ~500k rows; varchar(255) UTF8 column filename contains a file name;

                  我试图从文件名中去除各种奇怪的字符 - 我想我会使用一个字符类:[^a-zA-Z0-9()_ .\-]

                  I'm trying to strip out various strange characters out of the filename - thought I'd use a character class: [^a-zA-Z0-9()_ .\-]

                  现在,MySQL 中是否有一个函数可以让您通过正则表达式进行替换?我正在寻找与 REPLACE() 函数类似的功能 - 简化示例如下:

                  Now, is there a function in MySQL that lets you replace through a regular expression? I'm looking for a similar functionality to REPLACE() function - simplified example follows:

                  SELECT REPLACE('stackowerflow', 'ower', 'over');
                  
                  Output: "stackoverflow"
                  
                  /* does something like this exist? */
                  SELECT X_REG_REPLACE('Stackoverflow','/[A-Zf]/','-'); 
                  
                  Output: "-tackover-low"
                  

                  我知道REGEXP/RLIKE,但那些只检查if是否匹配,而不是什么匹配.

                  I know about REGEXP/RLIKE, but those only check if there is a match, not what the match is.

                  (我可以做一个SELECT pkey_id,filename FROM foo WHERE filename RLIKE '[^a-zA-Z0-9()_ .\-]'" 从 PHP 脚本中,执行 preg_replace 然后"UPDATE foo ... WHERE pkey_id=...",但这看起来像是最后的缓慢&丑陋的黑客)

                  (I could do a "SELECT pkey_id,filename FROM foo WHERE filename RLIKE '[^a-zA-Z0-9()_ .\-]'" from a PHP script, do a preg_replace and then "UPDATE foo ... WHERE pkey_id=...", but that looks like a last-resort slow & ugly hack)

                  推荐答案

                  使用 MySQL 8.0+ 你可以原生使用 REGEXP_REPLACE 函数.

                  With MySQL 8.0+ you could use natively REGEXP_REPLACE function.

                  12.5.2 正则表达式:

                  REGEXP_REPLACE(expr, pat, repl[, pos[,occurrence[, match_type]]])

                  用替换字符串repl替换字符串expr中匹配模式pat指定的正则表达式的出现,并返回结果细绳.如果exprpatreplNULL,则返回值为NULL.

                  Replaces occurrences in the string expr that match the regular expression specified by the pattern pat with the replacement string repl, and returns the resulting string. If expr, pat, or repl is NULL, the return value is NULL.

                  和正则表达式支持:

                  以前,MySQL 使用 Henry Spencer 正则表达式库来支持正则表达式运算符(REGEXPRLIKE).

                  Previously, MySQL used the Henry Spencer regular expression library to support regular expression operators (REGEXP, RLIKE).

                  正则表达式支持已使用 Unicode 国际组件 (ICU) 重新实现,该组件提供完整的 Unicode 支持并且是多字节安全的.REGEXP_LIKE() 函数以 REGEXPRLIKE 运算符的方式执行正则表达式匹配,它们现在是该函数的同义词.此外, REGEXP_INSTR() REGEXP_REPLACE() REGEXP_SUBSTR() 函数可用于查找匹配位置并分别执行子字符串替换和提取.

                  Regular expression support has been reimplemented using International Components for Unicode (ICU), which provides full Unicode support and is multibyte safe. The REGEXP_LIKE() function performs regular expression matching in the manner of the REGEXP and RLIKE operators, which now are synonyms for that function. In addition, the REGEXP_INSTR(), REGEXP_REPLACE(), and REGEXP_SUBSTR() functions are available to find match positions and perform substring substitution and extraction, respectively.

                  SELECT REGEXP_REPLACE('Stackoverflow','[A-Zf]','-',1,0,'c'); 
                  -- Output:
                  -tackover-low
                  

                  DBFiddle 演示

                  这篇关于如何在 MySQL 中进行正则表达式替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:我可以将多个 MySQL 行连接到一个字段中吗? 下一篇:MySQL中的排名函数

                  相关文章

                  最新文章

                    <bdo id='cBe3Y'></bdo><ul id='cBe3Y'></ul>
                  <i id='cBe3Y'><tr id='cBe3Y'><dt id='cBe3Y'><q id='cBe3Y'><span id='cBe3Y'><b id='cBe3Y'><form id='cBe3Y'><ins id='cBe3Y'></ins><ul id='cBe3Y'></ul><sub id='cBe3Y'></sub></form><legend id='cBe3Y'></legend><bdo id='cBe3Y'><pre id='cBe3Y'><center id='cBe3Y'></center></pre></bdo></b><th id='cBe3Y'></th></span></q></dt></tr></i><div id='cBe3Y'><tfoot id='cBe3Y'></tfoot><dl id='cBe3Y'><fieldset id='cBe3Y'></fieldset></dl></div>
                • <tfoot id='cBe3Y'></tfoot>

                      <legend id='cBe3Y'><style id='cBe3Y'><dir id='cBe3Y'><q id='cBe3Y'></q></dir></style></legend>

                      <small id='cBe3Y'></small><noframes id='cBe3Y'>