mysql-6正则表达式
mysql正则表达式
匹配的两种方式:
- 1.模糊匹配:like
- 2.正则表达式
正则表达式语法:
语法 | 说明 |
---|---|
^ | 起始位置。如果设置了RegExp对象的Multiline属性,^也匹配'\n'或'\r'之后的位置。 |
$ | 结束位置。如果设置了RegExp对象的Multiline属性,$也匹配'\n'或'\r'之前的位置。 |
. | 匹配处\n之外的任何单个字符。要匹配'\n'在内的任何字符,请使用'[.\n]'模式 |
[...] | 字符集合。匹配所包含的任意一个字符。例如'[abc]'可以匹配'plain'中的a |
[^...] | 负值字符集合。匹配未包含的任意字符。例如,'[^abc]'可以匹配'plain'中的'p' .这个不理解 |
p1|p2|p3 | 匹配p1或p2或p3。例如'z|food'能匹配"z"或"food"。'(z|f)ood'则匹配"zood"或"food"。 |
* | 匹配前面的子表达式0次或多次。'zo'能匹配"z"以及"zoo".等价于 |
+ | 匹配前面的子表达式1次或多次。'zo+'能匹配"zo"以及"zoo",但不能匹配"z".+等价于 |
n是一个非负整数。匹配确定的n次。例如'o{2}'不能匹配"Bob"中的"o",但能匹配"food"中的"oo" | |
m和n均为非负整数,其中n<=m。最少匹配n次且最多匹配m次 |
select * from csj_tbl where csj_author REGEXP "^菜鸟";
select * from csj_tbl where csj_author REGEXP "教程$";
select * from csj_tbl where csj_author REGEXP "^菜鸟教程$";
select * from csj_tbl where csj_author REGEXP "菜鸟教程.";
select * from csj_tbl where csj_title REGEXP "学习.java";
select * from csj_tbl where csj_title REGEXP "学习.java......";
select * from csj_tbl where csj_title REGEXP "[abc]";
select * from csj_tbl where csj_title REGEXP 'java|python';
-- 增加2条记录
insert into csj_tbl VALUES
(6,"zood","zoozoo",2018-11-12),
(7,"food","zoozoozoozoo",2018-11-12);
select * from csj_tbl where csj_title REGEXP "(z|f)ood";
select * from csj_tbl where csj_title REGEXP "oo*";
select * from csj_tbl where csj_title REGEXP "o+";
select * from csj_tbl where csj_author REGEXP "(zoo){2}";
select * from csj_tbl where csj_author REGEXP "(zoo){3,4}";
select * from csj_tbl where csj_author REGEXP '[^a-z0-9]';
-- 查找name字段中以元音字符开头或以'ok'字符串结尾的所有数据:
select * from person_tbl where name regexp '^[aeiou]|ok$';