mysql正则表达式正向_MySQL正则表达式regexp

1. 连接MySQL

基本字符匹配

SQL> select prod_name from products where prod_name regexp '1000' order by prod_name;

单字符匹配

SQL> select prod_name from products where prod_name regexp '.000' order by prod_name;

区分大小写

SQL> select prod_name from products where prod_name regexp binary 'JetPACK .000';

进行OR匹配

SQL> select prod_name from products where prod_name regexp '1000|2000' order by prod_name;

匹配几个字符之一

SQL> select prod_name from products where prod_name regexp '[123] Ton' order by prod_name;

SQL> select prod_name from products where prod_name regexp '[1|2|3] Ton' order by prod_name;

匹配范围

SQL> select prod_name from products where prod_name regexp '[0-5] Ton' order by prod_name;

匹配特殊字符

SQL> select vend_name from vendors where vend_name regexp '.' order by vend_name;

SQL> select vend_name from vendors where vend_name regexp '\\.' order by vend_name;

匹配多个实例

s? 表示 s可选

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

'[[:digit:]]{4}' 等价于 '[0-9][0-9][0-9][0-9]'

SQL> select prod_name from products where prod_name regexp '[[:digit:]]{4}' order by prod_name;

定位符

SQL> select prod_name from products where prod_name regexp '^[0-9\\.]' order by prod_name;

1. regexp 正则表达式

其中 .是正则表达式语言中一个特殊字符,表示匹配任意一个字符

2. binary 在正则表达式中用来区分大小写

3. | 表示或者,可多个连用

4. [] 匹配几个字符之一,[]中的|可以省略

[1|2|3] Ton可以缩写为[123] Ton,两者等价

[1|2|3] Ton和1|2|3 Ton不等价

5. - 可以用来定义范围

[0-9]等价于[0123456789],同理[a-z]

6. '.' 可以匹配特别的字符,例如 .、[]、|、- 等

为了匹配特殊字符,必须用\\为前导。\\-表示查找-,\\.表示查找.,\\\匹配\。另外

\\f 换页

\\n 换行

\\r 回车

\\t 制表

\\v 纵向制表

多数正则表达式实现使用单个反斜杠转义特殊字符,以便能使用这些字符本身。但MySQL要求两个反斜杠(MySQL自己解释一个,正则表达式库解释另一个)。

7. 匹配字符类

[:alnum:] 任意字母和数字(同[a-zA-Z0-9])

[:alpha:] 任意字符(同[a-zA-Z])

[:blank:] 空格和制表(同[\\t])

[:cntrl:] ASCII控制字符(ASCII 0到31和127)

[:digit:] 任意数字(同[0-9])

[:graph:] 与[:print:]相同,但不包括空格

[:lower:] 任意小写字母(同[a-z])

[:print:] 任意可以打印字符

[:punct:] 既不在[:alnum:]又不在[:cntrl:]中的任意字符

[:space:] 包括空格在内的任意空白字符(同[\\f\\n\\r\\t\\v])

[:upper:] 任意大写字母(同[A-Z])

[:xdigit:] 任意十六进制数字(同[a-fA-F0-9])

8. 匹配多个实例

* 0个或多个匹配

+ 1个或多个匹配(等于{1,})

? 0个或1个匹配(等于{0,1})

{n} 指定数目的匹配

{n,} 不少于制定数目的匹配

{n,m} 匹配数目的范围(m不超过255)

9. 定位符

^ 文本的开始

$ 文本的结尾

[[:<:>

[[:>:]] 词的结尾

10. regexp检查总是返回0(没有匹配)或者1(匹配)

例如 select 'hello' regexp '[0-9]'; 结果返回0

11. like匹配整个串,regexp匹配字串。

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2018-09-10 21:39

浏览 295

分类:数据库

评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值