微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Oracle正则表达式_小结

oracle中的支持正则表达式的函数主要有下面四个:


1
REGEXP_LIKE:与LIKE功能相似

2REGEXP_INSTR:与INSTR功能相似

3REGEXP_SUBSTR:与SUBSTR功能相似

4REGEXP_REPLACE:与REPLACE功能相似

regexp_like 只能用于条件表达式,和 like 类似,但是使用的正则表达式进行匹配,语法很简单:

regexp_substr 函数,和 substr 类似,用于拾取合符正则表达式描述的字符子串,语法如下:

上面这个模式参数就复杂了些 常用组合以下

regexp_substr(string,pattern,position)

ottom:0px;padding:0px;font-family:Arial;font-size:14px;white-space:normal;background-color:rgb(255,position,occurence)

ottom:0px;padding:0px;font-family:Arial;font-size:14px;white-space:normal;background-color:rgb(255,occurence,match_parameters)

regexp_instr 函数,和 instr 类似,用于标定符合正则表达式的字符子串的开始位置,语法如下:

http://www.weste.net/uploadfile/2010/0918/20100918104143534.gif

regexp_instr(string,pattern)

ottom:0px;padding:0px;font-family:Arial;font-size:14px;white-space:normal;background-color:rgb(255,return-option)

ottom:0px;padding:0px;font-family:Arial;font-size:14px;white-space:normal;background-color:rgb(255,return-option,parameters)

regexp_replace 函数,和 replace 类似,用于替换符合正则表达式的字符串,语法如下:

http://www.weste.net/uploadfile/2010/0918/20100918104144147.gif

这里解析一下几个参数的含义:

1。source_char,输入的字符串,可以是列名或者字符串常量、变量。

2。pattern,正则表达式。

3。match_parameter,匹配选项取值范围: i:大小写不敏感; c:大小写敏感;n:点号 . 不匹配换行符号;m:多行模式;x:扩展模式,忽略正则表达式中的空白字符。

4。position,标识从第几个字符开始正则表达式匹配。

5。occurrence,标识第几个匹配组。

6。replace_string,替换的字符串。

在新的函数中使用正则表达式来代替通配符‘%’和‘_’。

正则表达式由标准的元字符(Metacharacters)所构成:

'^'匹配输入字符串的开始位置,在方括号表达式中使用,此时它表示不接受该字符集合。

$'匹配输入字符串的结尾位置。如果设置了RegExp对象的Multiline属性,则$也匹配'n''r'

.匹配除换行符n之外的任何单字符。

?匹配前面的子表达式零次或一次。

+匹配前面的子表达式一次或多次。

*匹配前面的子表达式零次或多次。

'|'指明两项之间的一个选择。例子'^([a-z]+|[0-9]+)$'表示所有小写字母或数字组合成的字符串。

'( )'标记一个子表达式的开始和结束位置。

'[]'标记一个中括号表达式。

'{m,n}'一个精确地出现次数范围,m=<出现次数<=n'{m}'表示出现m次,表示至少出现m次。

num匹配,其中num一个正整数。对所获取的匹配的引用。

字符簇:

[[:alpha:]]任何字母。

[[:digit:]]任何数字。

[[:alnum:]]任何字母和数字。

[[:space:]]任何白字符。

[[:upper:]]任何大写字母。

[[:lower:]]任何小写字母。

[[:punct:]]任何标点符号

[[:xdigit:]]任何16进制的数字,相当于[0-9a-fA-F]各种操作符的运算优先级

转义符

(),(?:),(?=),[]圆括号和方括号

*,+,?,{n},{n,},m}限定符

^,$,anyMetacharacter位置和顺序

| “操作


说了一堆文绉绉的,现在开始实例演练了,在此之前先建好一个表。

createtabletmpas
withdataas(
select'like'asid,'a9999'asstrfromdualunionall
select'like','a9c'fromdualunionall
select'like','A7007'fromdualunionall
select'like','123a34cc'fromdualunionall
select'substr','123,234,345'fromdualunionall
select'substr','12,34.56:78'fromdualunionall
select'substr','123456789'fromdualunionall
select'instr','192.168.0.1'fromdualunionall
select'replace','(020)12345678'fromdualunionall
select'replace','001517729C28'fromdual
)
select*fromdata;
select*fromtmp;
IDSTR
--------------------
likea9999
likea9c
likeA7007
like123a34cc
substr123,345
substr12,34.56:78
substr123456789
instr192.168.0.1
replace(020)12345678
replace001517729C28

regexp_like 例子:

selectstrfromtmpwhereid='like'andregexp_like(str,'A\d+','i');--'i'忽略大小写
STR
-------------
a9999
a9c
A7007
123a34cc
selectstrfromtmpwhereid='like'andregexp_like(str,'a\d+');
STR
-------------
a9999
a9c
123a34cc
selectstrfromtmpwhereid='like'andregexp_like(str,'^a\d+');
STR
-------------
a9999
a9c
selectstrfromtmpwhereid='like'andregexp_like(str,'^a\d+c$');
STR
-------------
a9999


regexp_substr 例子:

colstrformata15;
select
str,regexp_substr(str,'[^,]+')str,]+',1,1)str,2)str,--occurrence第几个匹配组
regexp_substr(str,2,1)str--position从第几个字符开始匹配
fromtmp
whereid='substr';
STRSTRSTRSTRSTR
---------------------------------------------------------------------------
123,34512312323423
12,34.56:78121234.56:782
12345678912345678912345678923456789

select
str,'\d')str,'\d+','\d{2}','\d{3}',1)str
fromtmp
whereid='substr';
STRSTRSTRSTRSTR
---------------------------------------------------------------------------
123,345112323234
12,34.56:7811234
123456789112345678934234


selectregexp_substr('123456789','\d',level)str--取出每位数字,有时这也是行转列的方式
fromdual
connectbylevel<=9
STR
---------------
1
2
3
4
5
6
7
8
9

regex_instr 例子:


colindformat9999;
select
str,regexp_instr(str,'\.')ind,'\.',2)ind,5,2)ind
fromtmpwhereid='instr';
STRINdindIND
------------------------------
192.168.0.14810

select
regexp_instr('192.168.0.1',level)ind,--点号.所在的位置
regexp_instr('192.168.0.1',level)ind--每个数字的位置
fromdual
connectbylevel<=9
INdind
----------
41
82
103
05
06
07
09
011
00

regex_replace 例子:

select
str,regexp_replace(str,'020','GZ')str,'(\d{3})(\d{3})','<\2\1>')str--将第一、第二捕获组交换位置,用尖括号标识出来
fromtmp
whereid='replace';
STRSTRSTR
---------------------------------------------
(020)12345678(GZ)12345678(020)<456123>78
001517729C28001517729C28<517001>729C28

综合应用的例子:

colrow_lineformata30;
withsudokuas(
select'020000080568179234090000010030040050040205090070080040050000060289634175010000020'asline
fromdual
),tmpas(
selectregexp_substr(line,'\d{9}',level)row_line,levelcol
fromsudoku
connectbylevel<=9
)
selectregexp_replace(row_line,'(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)','\1\2\3\4\5\6\7\8\9')row_line
fromtmp

ROW_LINE
------------------------------
020000080
568179234
090000010
030040050
040205090
070080040
050000060
289634175
010000020

原文地址:https://www.jb51.cc/oracle/207916.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐