94人参与 • 2025-08-24 • 正则表达式

regexp (正则表达式) 是一种强大的字符串匹配工具,用于执行高级模式匹配操作。它提供了比简单通配符更精确、更灵活的文本搜索能力,特别适合处理复杂的字符串匹配场景。
主要特点:
select column_name from table_name where column_name regexp 'pattern';
其中:
column_name是要进行匹配的列名table_name是要查询的表名pattern是要匹配的正则表达式模式.:匹配任意单个字符(除了换行符)
a.c 匹配 "abc"、"aac"、"a1c" 等^:匹配字符串的开始位置^a 匹配以"a"开头的字符串$:匹配字符串的结束位置a$ 匹配以"a"结尾的字符串[]:指定一个字符集合,匹配其中的任意字符
[abc] 匹配"a"、"b"或"c"[a-z] 匹配任意小写字母[^]:指定一个不匹配的字符集合[^abc] 不匹配"a"、"b"或"c"*:匹配前面的模式零次或多次
ab*c 匹配"ac"、"abc"、"abbc"等+:匹配前面的模式一次或多次ab+c 匹配"abc"、"abbc",但不匹配"ac"?:匹配前面的模式零次或一次ab?c 匹配"ac"或"abc"{n}:精确匹配前面的模式n次a{2} 匹配"aa"{n,}:匹配前面的模式至少n次a{2,} 匹配"aa"、"aaa"等{n,m}:匹配前面的模式至少n次且不超过m次a{2,4} 匹配"aa"、"aaa"或"aaaa"|:逻辑"或"操作符
abc|def 匹配"abc"或"def"():用于组合模式a(bc)* 匹配"a"、"abc"、"abcbc"等-- 查找包含字母"a"的所有行 select * from products where product_name regexp 'a';
-- 查找以"pro"开头的产品 select * from products where product_name regexp '^pro'; -- 查找以"2023"结尾的订单号 select * from orders where order_id regexp '2023$';
-- 查找包含数字的电话号码 select * from customers where phone regexp '[0-9]'; -- 查找不包含元音字母的产品名称 select * from products where product_name regexp '[^aeiou]';
-- 查找包含"error"或"warning"的日志记录
select * from system_logs where message regexp 'error|warning';
-- 查找标准的电子邮件格式
select * from users where email regexp '^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,4}$';
-- 查找重复字母的单词
select * from words where word regexp '([a-za-z])\\1';
like 是sql中用于简单字符串匹配的操作符,使用通配符进行模式匹配。它适合简单的字符串搜索和匹配需求。
主要特点:
%和_select column_name from table_name where column_name like 'pattern';
其中:
% 匹配任意数量的字符(包括零个字符)_ 匹配单个任意字符%:匹配任意数量的字符(零个或多个)
a%:以"a"开头的字符串%a:以"a"结尾的字符串%a%:包含"a"的字符串_:匹配单个任意字符
a_c:匹配"abc"、"a1c"、"a c"等_a%:第二个字符是"a"的字符串-- 查找包含"book"的产品 select * from products where product_name like '%book%';
-- 查找以"mr."开头的人名 select * from customers where name like 'mr.%'; -- 查找以".com"结尾的邮箱 select * from users where email like '%.com';
-- 查找5个字符长度的产品代码 select * from products where product_code like '_____'; -- 查找第二和第三个字符为"12"的订单号 select * from orders where order_id like '_12%';
-- 查找包含百分号(%)的记录(使用转义) select * from data where notes like '%\%%' escape '\'; -- 查找包含下划线(_)的记录 select * from data where notes like '%\_%' escape '\';
避免在模式开头使用通配符:
-- 避免这样写(无法使用索引) select * from users where name like '%smith'; -- 改为这样写(可以使用索引) select * from users where name like 'smith%';
对于固定前缀的查询,考虑使用范围查询:
-- 不如 select * from products where product_name like 'a%'; -- 可以考虑 select * from products where product_name >= 'a' and product_name < 'b';
对于简单的相等匹配,使用=而非like:
-- 更好 select * from users where username = 'admin'; -- 不如 select * from users where username like 'admin';
| 特性 | regexp | like |
|---|---|---|
| 功能 | 强大,支持复杂模式 | 简单,仅支持基本通配符 |
| 性能 | 通常较慢 | 通常较快 |
| 通配符 | 丰富的元字符(. ^ $ []等) | 仅%和_ |
| 学习曲线 | 较陡峭 | 较简单 |
| 索引使用 | 一般不能使用索引 | 前缀匹配可以使用索引 |
| 适用场景 | 复杂模式匹配 | 简单字符串搜索 |
使用like的情况:
-- 适合使用like的示例 select * from customers where name like 'john%';
使用regexp的情况:
-- 适合使用regexp的示例
select * from products where product_code regexp '^[a-z]{2}[0-9]{4}$';
混合使用场景: 在某些情况下,可以结合使用两者以获得最佳性能和灵活性:
-- 先用like缩小范围,再用regexp精确匹配
select * from logs
where message like '%error%'
and message regexp 'error [0-9]{3}';
假设有一个包含100万条记录的用户表:
-- like查询(较快,可以使用索引) select * from users where username like 'admin%'; -- 等效的regexp查询(较慢,无法使用索引) select * from users where username regexp '^admin';
对于这个简单的前缀匹配,like通常比regexp快10-100倍,特别是在有适当索引的情况下。
避免过度复杂的模式:复杂的正则表达式会显著降低查询速度
使用非贪婪匹配:在支持的情况下,使用*?或+?进行非贪婪匹配
-- 匹配最短的可能结果 select * from text where content regexp '<div>.*?</div>';
预编译正则表达式:某些数据库支持预编译正则表达式以提高性能
escape子句:处理包含通配符本身的字符串
select * from data where notes like '50\%%' escape '\';
字符集处理:考虑数据库的字符集和排序规则对匹配的影响
大小写敏感:大多数数据库的like是大小写敏感的,但可以通过设置改变
mysql:
postgresql:
~运算符进行正则匹配!~运算符进行不匹配操作sql server:
oracle:
-- 验证电子邮件格式
select * from users where email not regexp '^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\\.[a-za-z]{2,4}$';
-- 验证电话号码格式
select * from customers where phone regexp '^[0-9]{3}-[0-9]{3}-[0-9]{4}$';
-- 查找并替换无效字符
update products
set product_name = regexp_replace(product_name, '[^a-za-z0-9 ]', '')
where product_name regexp '[^a-za-z0-9 ]';
-- 标准化日期格式
update orders
set order_date = str_to_date(
regexp_substr(order_date_field, '[0-9]{4}-[0-9]{2}-[0-9]{2}'),
'%y-%m-%d'
)
where order_date_field regexp '[0-9]{4}-[0-9]{2}-[0-9]{2}';
-- 查找特定错误模式的日志记录
select * from server_logs
where log_message regexp 'error (500|503|504):';
-- 按错误类型分类统计
select
regexp_substr(error_message, '[a-za-z]+error') as error_type,
count(*) as count
from application_logs
where error_message regexp '[a-za-z]+error'
group by error_type;
-- 高级产品搜索(支持多种匹配方式)
select * from products
where
(product_name like '%organic%' or description like '%organic%')
and product_code regexp '^[a-z]{2}[0-9]{3}'
and price between 10 and 100;
基本原则:
性能最佳实践:
可读性建议:
测试策略:
通过合理选择和使用regexp与like,可以高效地处理各种字符串匹配需求,同时保持查询性能和代码可维护性。
到此这篇关于正则表达式(regexp)与通配符(like)超详细对比的文章就介绍到这了,更多相关regexp与like对比内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论