MySQL 正则表达式

本文详细介绍了如何在MySQL中使用正则表达式进行数据查询,包括基本字符匹配、OR匹配、字符类匹配、范围匹配以及特殊字符和定位符的使用。通过实例演示了如何筛选以特定数字开头的产品名称,以及如何匹配包含特定字符组合的记录。正则表达式是数据库查询中的强大工具,能帮助开发者实现复杂的数据过滤需求。
摘要由CSDN通过智能技术生成

基本字符匹配

关键字:regexp

select prod_name from products
where prod_name regexp "1000";

image

regexp后所跟的东西作为正则表达式处理

select prod_name from products
where prod_name regexp ".000";

image
.是正则表达式中一个特殊的字符。它表示匹配任意一个字符。

进行or匹配

select prod_name from products
where prod_name regexp "1000|2000|3000";

image

|为正则表达式的or操作符

匹配几个字符之一

select prod_name from products
where prod_name regexp '[12345] ton';

image

[12345]表示匹配1,2,3,4,5中的一个字符。即:or语句的另一表达方法。

反例:

select prod_name from products
where prod_name regexp '1|2|3 ton';

image

1|2|3 ton表示1或2或3 ton

select prod_name from products
where prod_name regexp '[^123] ton';

image

[^123]表示除去1,2,3之外的其他字符

匹配范围

[1-9]表示[123456789]

[a-z]表示任意字母

匹配特殊字符

为匹配特殊字符必须用\\为前导。如:\\-表示查找-。\\也用来引用元字符

匹配字符类

image

匹配多个实例

image

select prod_name from products
where prod_name regexp '\\([0-9] sticks?\\)';

image
其中,\\(表示匹配(;[0-9]表示匹配任意0-9字符;s?表示匹配0个s或1个s;\\)表示匹配)

定位符

image

select prod_name from products
where prod_name regexp '^[0-9]';

image
表示匹配以数字开头的字符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值