博客
关于我
2.正则表达式的使用
阅读量:809 次
发布时间:2019-03-25

本文共 1729 字,大约阅读时间需要 5 分钟。

基本字符匹配

在SQL中,你可以使用regexp函数来匹配数据库中的字段值。在基本字符匹配中,regexp接受一个模式字符串,匹配字段中的字符。

示例

select columns from table where columns regexp '字符';select columns from table where columns regexp '字符|字符';

批量字符匹配

为了匹配多个字符中的任意一个,可以使用|符号将要匹配的字符用[]括起来。

示例

select name from products where name regexp '[123] tom ';

这个模式会匹配1、2或3 tom的字符。括号内的匹配范围可以是[1-9]、[a-z]、[A-Z]等。

示例

select name from products where name regexp '[1-5] tom ';

该模式匹配1到5之间的任意一位数字再跟随" tom"。

特殊字符匹配

regexp中,以下特殊字符需要注意:

  • .:匹配任意字符,除了换行符
  • []:匹配空白字符,\]用于匹配闭区间,[用于开区间
  • |:作为或的逻辑符号使用,默认是匹配任意字符
  • _:在某些正则表达式中,下划线默认匹配任意字符

示例

select name from products where name regexp '\\[0-9\\] sticks?';

这一行代码首先匹配任意数字,然后匹配sticks?。因为?表示零个或一个"s",因此"sticks""sticks "都能被匹配。

空白元字符

regexp中,以下空白元字符需要特别处理:

  • \f:换页符
  • \n:换行符
  • \r:回车符
  • \t:制表符
  • \v:纵向制表符
  • 如果要匹配反斜杠(\"),需要使用\\表示

字符类

regexp中提供了多种字符类,用方括号指定:

  • [:alnum:]:任意字母和数字
  • [:alpha:]:任意字母(大小写)
  • [:blank:]:空格或制表符
  • [:cntrl:]:ASCII控制字符(0-31和127)
  • [:digit:]:任意数字
  • [:graph:]:与[:print:]相同,但不包括空格
  • [:lower:]:任意小写字母
  • [:print:]:任意可打印字符
  • [:punct:]:既不在[:alnum:]又不在[:cntrl:]中的任意字符
  • [:space:]:包括空格在内的任意字符
  • [:upper:]:任意大写字母
  • [:xdigit:]:任意十六进制数字(0-9和a-f/A-F)

示例

select name from products where name regexp '[[:digit:]] {4}';

这个模式会匹配[:digit:](任意数字)连续出现4次的字符串。

重复元字符

重复元字符用于控制匹配次数:

  • *:0个或多个匹配
  • +:1个或多个匹配
  • ?:0个或1个匹配
  • {n}:指定n个匹配
  • {n,}:不少于n个匹配
  • {n,m}:匹配n到m个之间的字符

示例

select name from product where name regexp '[0-9] sticks?';

这里,[0-9]匹配任意数字,sticks?匹配stickssticks

定位元字符

定位元字符用来指定匹配的位置:

  • ^:文本的开始
  • $:文本的结尾
  • [[:<:] Broi):词的开始
  • [[:>:] Broi):词的结尾

示例

select name from product where name regexp '^[0-9\\.]';

这里,^只在文本开始时才匹配,而不仅仅是任意位置。

regexp与like的区别

regexplike在模式应用上存在差异:

  • like匹配整个字段,而regexp默认匹配子串。
  • 使用^$可以使regexp的作用与like类似。

示例

select name from product where name regexp '\\[0-9\\] sticks?';

在这个例子中,^[0-9\\.]意味着匹配从文本开始并以数字或者小数结尾的字符串。

转载地址:http://fzbyk.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>