正则表达式学习网址:
正则表达式 – 教程 | 菜鸟教程
正则表达式在线测试:
正则表达式在线测试 | 菜鸟工具

MySQL支持的正则表达式仅仅是正则表达式众多实现的一个子集,在使用正则表达式之前,建议先测试一下。

  1. mysql> select '我爱你中国' regexp '我爱你';
  2. +------------------------------+
  3. | '我爱你中国' regexp '我爱你' |
  4. +------------------------------+
  5. | 1 |
  6. +------------------------------+
mysql> select '12306' regexp '[:digit:]';
+----------------------------+
| '12306' regexp '[:digit:]' |
+----------------------------+
|                          1 |
+----------------------------+

表达式的使用

数据准备

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `product_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '产品名称',
  `price` decimal(10, 2) UNSIGNED NOT NULL COMMENT '产品价格',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of product
-- ----------------------------
INSERT INTO `product` VALUES (1, 'Apple iPhone 13 (A2634)', 6799.00);
INSERT INTO `product` VALUES (2, 'HUAWEI P50 Pro', 6488.00);
INSERT INTO `product` VALUES (3, 'MIX4', 4999.00);
INSERT INTO `product` VALUES (4, 'OPPO Find X3', 3999.00);
INSERT INTO `product` VALUES (5, 'vivo X70 Pro+', 5999.00);

SET FOREIGN_KEY_CHECKS = 1;

初始数据如下:

mysql> select * from product;
+----+-------------------------+---------+
| id | product_name            | price   |
+----+-------------------------+---------+
|  1 | Apple iPhone 13 (A2634) | 6799.00 |
|  2 | HUAWEI P50 Pro          | 6488.00 |
|  3 | MIX4                    | 4999.00 |
|  4 | OPPO Find X3            | 3999.00 |
|  5 | vivo X70 Pro+           | 5999.00 |
+----+-------------------------+---------+

正则表达式的作用是文本匹配,使用一个正则表达式与一个文本内容进行比较,可以校验文本是否符合正则表达式阐述的规则。
在MySQL中,正则表达式用在where子句中,可以对select查询的数据进行过滤。
select * from table_name where regexp ‘你的正则表达式’ order by cloumn_name;

查询产品表中产品名称中包含3的产品

mysql> select * from product where product_name regexp '3';
+----+-------------------------+---------+
| id | product_name            | price   |
+----+-------------------------+---------+
|  1 | Apple iPhone 13 (A2634) | 6799.00 |
|  4 | OPPO Find X3            | 3999.00 |
+----+-------------------------+---------+

区分大小写

MySQL使用正则表达式默认不区分大小写,但是大部分情况下我们需要明确英文的大小写进行匹配,这个时候我们可以使用binary关键字。

查询产品表中,产品名称包含huawei的产品
默认情况:

mysql> select * from product where product_name regexp 'huawei';
+----+----------------+---------+
| id | product_name   | price   |
+----+----------------+---------+
|  2 | HUAWEI P50 Pro | 6488.00 |
+----+----------------+---------+

区分大小写:

mysql> select * from product where product_name regexp binary 'huawei';
Empty set (0.00 sec)

正则表达式与like的区别

正则表达式是一个非常强大的文本检索过滤工具,它的所能实现的功能比like操作符强大,like能做的正则表达式都能做。

example:查询产品表中,产品名称中v至少出现一次产品信息(like不能实现)

mysql> select * from product where product_name regexp 'v+';
+----+---------------+---------+
| id | product_name  | price   |
+----+---------------+---------+
|  5 | vivo X70 Pro+ | 5999.00 |
+----+---------------+---------+

[

](https://blog.csdn.net/qq_41125219/article/details/121298909)
注意:正则表达式重复元字符的匹配都是整个连续出现。下面给出这几个重复元字符。

元字符 说明
* 0个或多个匹配,效果与{0,}一致
+ 1个或多个匹配,效果与{1,}一致
1个或0匹配,效果与{0,1}一致
{n} 等于n个匹配数目
{n,} 大于等于n个匹配
{n,m} 大于等于n小于等于m个,m<255