mysql 怎么使用正则表达式

如何在 MySQL 中的正则表达式替换~

mysql的正则匹配用regexp,而替换字符串用REPLACE(str,from_str,to_str)
举例如下:
UPDATE myTable SET HTML=REPLACE(HTML,'','') WHERE HTML REGEXP '(\s*){2,}'
达到的效果:会把所有全部替换掉。
mysql中常用的替换函数
所用到的函数:
locate:
LOCATE(substr,str)
POSITION(substr IN str)
返回子串 substr 在字符串 str 中第一次出现的位置。如果子串 substr 在 str 中不存在,返回值为 0:
substring
SUBSTR(str,pos,len): 由中的第位置开始,选出接下去的个字元。
replace
replace(str1, str2, str3): 在字串 str1 中,当str2 出现时,将其以 str3 替代。

mysql> select @col;
+---------------------------+
| @col |
+---------------------------+
| 2.1.3.14单独的3中文3中文1 |
+---------------------------+
1 row in set

mysql> select concat(substring_index(@col, ".", 3), ".", replace(@col, concat(substring_index(@col, ".", 3), "."), "")+0) as result;
+----------+
| result |
+----------+
| 2.1.3.14 |
+----------+
1 row in set


你把第二句中的 @col 替换成你的列名就可以了,然后加上 from 表 成为一个完成的 sql 语句试试吧。

属性名 regexp ‘匹配方式'

正则表达式的模式字符

^ 匹配字符开始的部分

eg1: 从info表name字段中查询以L开头的记录

select * from info where name regexp '^L';

eg2: 从info表name字段中查询以aaa开头的记录

select * from info where name regexp '^aaa';

$ 匹配字符结束的部分

eg1: 从info表name字段中查询以c结尾的记录

select * from info where name regexp 'c$';

eg2: 从info表name字段中查询以aaa结尾的记录

select * from info where name regexp 'aaa$';

. 匹配字符串中的任意一个字符,包括回车和换行

eg1: 从info表name字段中查询以L开头y结尾中间有两个任意字符的记录

select * from info where name regexp '^L..y$';

[字符集合]匹配字符集合中的任意字符

eg1: 从info表name字段中查询包含c、e、o三个字母中任意一个的记录

select * from info where name regexp '[ceo]';

eg2: 从info表name字段中查询包含数字的记录

select * from info where name regexp '[0-9]';

eg3: 从info表name字段中查询包含数字或a、b、c三个字母中任意一个的记录

select * from info where name regexp '[0-9a-c]';

[^字符集合]匹配除了字符集合外的任意字符

eg1: 从info表name字段中查询包含a-w字母和数字以外字符的记录

select * from info where name regexp '[^a-w0-9]';

s1|s2|s3 匹配s1s2s3中的任意一个

eg1: 从info表name字段中查询包含'ic'的记录

select * from info where name regexp 'ic';

eg2: 从info表name字段中查询包含ic、uc、ab三个字符串中任意一个的记录

select * from info where name regexp 'ic|uc|ab';

* 代表多个该字符前的字符,包括0个或1个

eg1: 从info表name字段中查询c之前出现过a的记录

select * from info where name regexp 'a*c';

+ 代表多个该字符前的字符,包括1个

eg1: 从info表name字段中查询c之前出现过a的记录

select * from info where name regexp 'a+c';(注意比较结果!)

字符串{N} 字符串出现N次

eg1: 从info表name字段中查询出现过a3次的记录

select * from info where name regexp 'a{3}';

字符串{M,N}字符串最少出现M次,最多出现N次

eg1: 从info表name字段中查询ab出现最少1次最多3次的记录

select * from info where name regexp 'ab{1,3}';

MYSQL中自带通配符(LIKE关键词)

%可以表示任意长度的字符(包括0)

-可以表示单个字符

MySQL 正则表达式

  • 在前面的章节我们已经了解到MySQL可以通过 LIKE …% 来进行模糊匹配。

  • MySQL 同样也支持其他正则表达式的匹配, MySQL中使用 REGEXP 操作符来进行正则表达式匹配。

  • 如果您了解PHP或Perl,那么操作起来就非常简单,因为MySQL的正则表达式匹配与这些脚本的类似。

下表中的正则模式可应用于 REGEXP 操作符中。

from 树懒学堂 - 一站式数据知识平台



MySQL 一直以来都支持正则匹配,不过对于正则替换则一直到MySQL 8.0 才支持。对于这类场景,以前要么在MySQL端处理,要么把数据拿出来在应用端处理。

比如我想把表y1的列str1的出现第3个action的子 串替换成dble,怎么实现?


1. 自己写SQL层的存储函数。代码如下写死了3个,没有优化,仅仅作为演示,MySQL 里非常不建议写这样的函数。

  • mysql

  • DELIMITER $$

  • USE `ytt`$$

  • DROP FUNCTION IF EXISTS `func_instr_simple_ytt`$$

  • CREATE DEFINER=`root`@`localhost` FUNCTION `func_instr_simple_ytt`(

  • f_str VARCHAR(1000), -- Parameter 1

  • f_substr VARCHAR(100),  -- Parameter 2

  • f_replace_str varchar(100),

  • f_times int -- times counter.only support  3.

  • ) RETURNS varchar(1000)

  • BEGIN

  • declare v_result varchar(1000) default 'ytt'; -- result.

  • declare v_substr_len int default 0; -- search string length.

  • set f_times = 3; -- only support  3.

  • set v_substr_len = length(f_substr);

  • select instr(f_str,f_substr) into @p1; -- First real position .

  • select instr(substr(f_str,@p1+v_substr_len),f_substr) into @p2; Secondary virtual position.

  • select instr(substr(f_str,@p2+ @p1 +2*v_substr_len - 1),f_substr) into @p3; -- Third virtual position.

  • if @p1 > 0  && @p2 > 0 && @p3 > 0 then -- Fine.

  • select

  • concat(substr(f_str,1,@p1 + @p2 + @p3 + (f_times - 1) * v_substr_len  - f_times)

  • ,f_replace_str,

  • substr(f_str,@p1 + @p2 + @p3 + f_times * v_substr_len-2)) into v_result;

  • else

  • set v_result = f_str; -- Never changed.

  • end if;

  • -- Purge all session variables.

  • set @p1 = null;

  • set @p2 = null;

  • set @p3 = null;

  • return v_result;

  • end;

  • $$

  • DELIMITER ;

  • -- 调用函数来更新:

  • mysql> update y1 set str1 = func_instr_simple_ytt(str1,'action','dble',3);

  • Query OK, 20 rows affected (0.12 sec)

  • Rows matched: 20  Changed: 20  Warnings: 0

  • 2. 导出来用sed之类的工具替换掉在导入,步骤如下:(推荐使用)

    1)导出表y1的记录。

  • mysqlmysql> select * from y1 into outfile '/var/lib/mysql-files/y1.csv';Query OK, 20 rows affected (0.00 sec)


  • 2)用sed替换导出来的数据。

  • shellroot@ytt-Aspire-V5-471G:/var/lib/mysql-files#  sed -i 's/action/dble/3' y1.csv


  • 3)再次导入处理好的数据,完成。

  • mysql

  • mysql> truncate y1;

  • Query OK, 0 rows affected (0.99 sec)

  • mysql> load data infile '/var/lib/mysql-files/y1.csv' into table y1;

  • Query OK, 20 rows affected (0.14 sec)

  • Records: 20  Deleted: 0  Skipped: 0  Warnings: 0

  • 以上两种还是推荐导出来处理好了再重新导入,性能来的高些,而且还不用自己费劲写函数代码。

    那MySQL 8.0 对于以上的场景实现就非常简单了,一个函数就搞定了。

  • mysqlmysql> update y1 set str1 = regexp_replace(str1,'action','dble',1,3) ;Query OK, 20 rows affected (0.13 sec)Rows matched: 20  Changed: 20  Warnings: 0


  • 还有一个regexp_instr 也非常有用,特别是这种特指出现第几次的场景。比如定义 SESSION 变量@a。

  • mysqlmysql> set @a = 'aa bb cc ee fi lucy  1 1 1 b s 2 3 4 5 2 3 5 561 19 10 10 20 30 10 40';Query OK, 0 rows affected (0.04 sec)


  • 拿到至少两次的数字出现的第二次子串的位置。

  • mysqlmysql> select regexp_instr(@a,'[:digit:]{2,}',1,2);+--------------------------------------+| regexp_instr(@a,'[:digit:]{2,}',1,2) |+--------------------------------------+|                                   50 |+--------------------------------------+1 row in set (0.00 sec)


  • 那我们在看看对多字节字符支持如何。

  • mysql

  • mysql> set @a = '中国 美国 俄罗斯 日本 中国 北京 上海 深圳 广州 北京 上海 武汉 东莞 北京 青岛 北京';

  • Query OK, 0 rows affected (0.00 sec)

  • mysql> select regexp_instr(@a,'北京',1,1);

  • +-------------------------------+

  • | regexp_instr(@a,'北京',1,1)   |

  • +-------------------------------+

  • |                            17 |

  • +-------------------------------+

  • 1 row in set (0.00 sec)

  • mysql> select regexp_instr(@a,'北京',1,2);

  • +-------------------------------+

  • | regexp_instr(@a,'北京',1,2)   |

  • +-------------------------------+

  • |                            29 |

  • +-------------------------------+

  • 1 row in set (0.00 sec)

  • mysql> select regexp_instr(@a,'北京',1,3);

  • +-------------------------------+

  • | regexp_instr(@a,'北京',1,3)   |

  • +-------------------------------+

  • |                            41 |

  • +-------------------------------+

  • 1 row in set (0.00 sec)

  • 那总结下,这里我提到了 MySQL 8.0 的两个最有用的正则匹配函数 regexp_replace 和 regexp_instr。针对以前类似的场景算是有一个完美的解决方案。



相关兴趣推荐

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网