大佬教程收集整理的这篇文章主要介绍了MySQL REGEXP:正则表达式查询,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
选项 | 说明 | 例子 | 匹配值示例 |
---|---|---|---|
^ | 匹配文本的开始字符 | '^b' 匹配以字母 b 开头 的字符串 | book、big、banana、 bike |
$ | 匹配文本的结束字符 | 'st$’ 匹配以 st 结尾的字 符串 | test、resist、persist |
. | 匹配任何单个字符 | 'b.t’ 匹配任何 b 和 t 之间有一个字符 | bit、bat、but、bite |
* | 匹配零个或多个在它前面的字 符 | 'f*n’ 匹配字符 n 前面有 任意个字符 f | fn、fan、faan、abcn |
+ | 匹配前面的字符 1 次或多次 | 'ba+’ 匹配以 b 开头,后 面至少紧跟一个 a | ba、bay、bare、battle |
<字符串> | 匹配包含指定字符的文本 | 'fa’ | fan、afa、faad |
[字符集合] | 匹配字符集合中的任何一个字 符 | '[xz]'匹配 x 或者 z | dizzy、zebra、x-ray、 extra |
[^] | 匹配不在括号中的任何字符 | '[^abc]’ 匹配任何不包 含 a、b 或 c 的字符串 | desk、fox、f8ke |
字符串{n,} | 匹配前面的字符串至少 n 次 | b{2} 匹配 2 个或更多 的 b | bbb、 @R_675_9779@、 @R_675_9779@bbb |
字符串 {n,m} |
匹配前面的字符串至少 n 次, 至多 m 次 | b{2,4} 匹配最少 2 个, 最多 4 个 b | bbb、 @R_675_9779@ |
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '^C'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 1 | Computer | 11111 | A | | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 2 rows in set (0.05 seC)在 tb_departments 表中有两条记录的 dept_name 字段值是以字母 C 开头的,返回结果有 2 条记录。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '^Ch'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 1 row in set (0.03 seC)只有 Chinese 是以“Ch”开头的,所以查询结果中只有 1 条记录。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP 'y$'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 4 | Economy | 44444 | B | | 5 | History | 55555 | B | +---------+-----------+-----------+-----------+ 2 rows in set (0.00 seC)在 tb_departments 表中有两条记录的 dept_name 字段值是以字母 y 结尾的,返回结果有 2 条记录。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP 'my$'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 4 | Economy | 44444 | B | +---------+-----------+-----------+-----------+ 1 row in set (0.00 seC)只有 Economy 是以“my”结尾的,所以查询结果中只有 1 条记录。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP 'o.y'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 4 | Economy | 44444 | B | | 5 | History | 55555 | B | +---------+-----------+-----------+-----------+ 2 rows in set (0.00 seC)查询语句中“o.y”指定匹配字符中要有字母 o 和 y,且两个字母之间包含单个字符,并不限定匹配的字符的位置和所在查询字符串的总长度,因此 Economy 和 History 都符合匹配条件。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '^Ch*'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 1 | Computer | 11111 | A | | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 2 rows in set (0.00 seC)星号“*”可以匹配任意多个字符,Computer 中字母 C 后面并没有出现字母 h,但是也满足匹配条件。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '^Ch+'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 1 row in set (0.00 seC)“h+”匹配字母“h”至少一次,只有 Chinese 满足匹配条件。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP 'in'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 1 row in set (0.00 seC)可以看到,dept_name 字段的 Chinese 中包含字符串“in”,满足匹配条件。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP 'in|on'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 3 | Chinese | 33333 | B | | 4 | Economy | 44444 | B | +---------+-----------+-----------+-----------+ 2 rows in set (0.00 seC)可以看到,dept_name 字段的 Chinese 中包含字符串“in”,Economy 中包含字符串“on”,满足匹配条件。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '[io]'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 1 | Computer | 11111 | A | | 3 | Chinese | 33333 | B | | 4 | Economy | 44444 | B | | 5 | History | 55555 | B | +---------+-----------+-----------+-----------+ 4 rows in set (0.00 seC)从查询结果可以看到,所有返回的记录的 dept_name 字段的值中都包含字母 o 或者 e,或者两个都有。
MysqL> SELECT * FROM tb_departments -> WHERE dept_call REGEXP '[123]'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 1 | Computer | 11111 | A | | 2 | Math | 22222 | A | | 3 | Chinese | 33333 | B | +---------+-----------+-----------+-----------+ 3 rows in set (0.00 seC)查询结果中,dept_call 字段值中有 1、2、3 三个数字中的一个即为匹配记录字段。
MysqL> SELECT * FROM tb_departments -> WHERE dept_name REGEXP '[^a-t]'; +---------+-----------+-----------+-----------+ | dept_id | dept_name | dept_call | dept_type | +---------+-----------+-----------+-----------+ | 1 | Computer | 11111 | A | | 4 | Economy | 44444 | B | | 5 | History | 55555 | B | +---------+-----------+-----------+-----------+ 3 rows in set (0.00 seC)返回记录中的 dept_name 字段值中包含了指定字母和数字以外的值,如 u、y 等,这些字母均不在 a~t 中,满足匹配条件。
以上是大佬教程为你收集整理的MySQL REGEXP:正则表达式查询全部内容,希望文章能够帮你解决MySQL REGEXP:正则表达式查询所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。