首页 > 数据库 >mysql正则替换 正宗!

mysql正则替换 正宗!

时间:2023-02-20 15:46:02浏览次数:37  
标签:string expr REPLACE 正则 mysql REGEXP 正宗 match

先看个官方例子

mysql> SELECT REGEXP_REPLACE('a b c', 'b', 'X');
+-----------------------------------+
| REGEXP_REPLACE('a b c', 'b', 'X') |
+-----------------------------------+
| a X c                             |
+-----------------------------------+
mysql> SELECT REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3);
+----------------------------------------------------+
| REGEXP_REPLACE('abc def ghi', '[a-z]+', 'X', 1, 3) |
+----------------------------------------------------+
| abc def X                                          |
+----------------------------------------------------+

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

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.

REGEXP_REPLACE() takes these optional arguments:

pos: The position in expr at which to start the search. If omitted, the default is 1.

occurrence: Which occurrence of a match to replace. If omitted, the default is 0 (which means “replace all occurrences”).

match_type: A string that specifies how to perform matching. The meaning is as described for REGEXP_LIKE().

Prior to MySQL 8.0.17, the result returned by this function used the UTF-16 character set; in MySQL 8.0.17 and later, the character set and collation of the expression searched for matches is used. (Bug #94203, Bug #29308212)

For additional information about how matching occurs, see the description for REGEXP_LIKE().

来个人干的事情:

SELECT REGEXP_REPLACE('哇啦哇啦(报警)', '(.*?)((.*))', '$1$2');
| 哇啦哇啦报警

看来也是支持分组捕获的

标签:string,expr,REPLACE,正则,mysql,REGEXP,正宗,match
From: https://www.cnblogs.com/bigjor/p/17137667.html

相关文章

  • MySQL的varchar定义长度到底是字节还是字符,varchar字符长度的计算
    1.在开始之前先简单介绍下字符和字节的区别:字符人们使用的记号,抽象意义上的一个符号。一个汉字和英文就是一个字符,如'1','中','a','$','¥',……字节计算机中存储......
  • MySQL中length()、char_length()的区别和用法
    方法概述:char_length(str)计算单位:字符不管汉字还是数字或者是字母都算是一个字符length(str)计算单位:字节utf8编码:一个汉字三个字节,一个数字或字母一个字节。gbk......
  • MySQL性能调优必知:Performance Schema引擎的配置与使用
    当你在MySQL高并发情况下的进行性能调优时,需要知道调整后的影响。例如查询是否变快了?锁是否会减慢运行速度?内存使用情况如何?磁盘IO等待时间变了吗?.PerformanceSchema就......
  • MySQL 创建数据库
    1.1进入MySQL命令:mysql-utest-p;1.2查看数据库命令:SHOWDATABASES;1.3新建数据库命令:CREATEDATABASEitem_name;1.4验证是否查看成功命令:SHOW......
  • 解决Mac下pip install mysqlclient 时的报错
    Django使用Mysql需要安装mysqlclient,在Mac下pipinstallmysqlclient时部分报错如下:Completeoutput(15lines):/bin/sh:mysql_config:commandnotfound/......
  • 00023.07 IP地址、端口号在代码中的使用(IntAdddress、各种数据库注册端口号Oracle(1521
    系列文章目录文章目录​​系列文章目录​​​​一、IP地址​​​​二、端口号​​​​三、IP地址、端口号代码当中的表示​​​​总结​​一、IP地址IP地址分为IPV4和IPV6......
  • Mysql基础
    sql分类DDL:数据定义语言create、alter、drop、rename、truncateDML:数据操作语言insert、delete、update、selectDCL:数据控制语言commit、rollback、savepoint、grant......
  • 正则的扩展详解
    RegExp()在es5中,RegExp的构造函数参数有两种情况1、字符串2、正则表达式//第一种情况letregex=newRegExp('abc','i')//第二种情况letregex2=/abc/i这......
  • MYSQL悲观锁-用户余额
    无论什么锁JAVA的synchronized也好,还是MYSQL的锁都好,要注意分布式环境与单机环境1.乐观锁乐观认为并发不高,甚至没有并发。其中一种实现方式依靠在表中多加一个版本号字段......
  • Docker中Mysql容器忘记密码的处理方法
    今天非常的倒霉,因为学习了Vue的相关知识,想自己写一个后端服务器来练习一下Vue然后忘记了Docker中Mysql的密码。。。很抽象下面是我的解决方法一、如果在本地的Navica......