您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

oracle的正则表达式,

bubuko 2022/1/25 20:04:18 其他 字数 8461 阅读 675 来源 http://www.bubuko.com/infolist-5-1.html

根据定义的规律进行数据的检索、添加、删除、分析、叠加、插入和修整。 Oracle使用正则表达式离不开这4个函数: 1。regexp_like 2。regexp_substr 3。regexp_instr 4。regexp_replace 看函数名称大概就能猜到有什么用了。 1.regexp_like ...

根据定义的规律进行数据的检索、添加、删除、分析、叠加、插入和修整。

 

Oracle使用正则表达式离不开这4个函数:

1。regexp_like

2。regexp_substr

3。regexp_instr

4。regexp_replace

看函数名称大概就能猜到有什么用了。

 

1.regexp_like 只能用于条件表达式,和 like 类似,但是使用的正则表达式进行匹配,

语法很简单:

regexp_like(source_char,pattern,match_parameter)

 

 

 

 

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

regexp_substr(source_char,pattern,position,occurrence,match_parameter)

 

 

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

regexp_instr(source_char,pattern,position,occurrence,return_option,match_parameter)

 

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

regexp_replace(source_char,pattern,replace_string,position,occurrence,match_parameter)

 

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

1。source_char,输入的字符串源,

2。pattern,正则表达式。

3。match_parameter,匹配选项。

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

4。position,标识从第几个字符开始正则表达式匹配。可选。搜索在字符串中的开始位置。如果省略,默认为1,这是第一个位置的字符串。

5。occurrence,标识第几个匹配组。可选。它是模式字符串中的第n个匹配位置。如果省略,默认为1。

6。replace_string,替换的字符串。

 

描述
1.^ 匹配一个字符串的开始。如果与“m” 的match_parameter一起使用,则匹配表达式中任何位置的行的开头。
2.$ 匹配字符串的结尾。如果与“m” 的match_parameter一起使用,则匹配表达式中任何位置的行的末尾。
3.* 匹配零个或多个。
4.+ 匹配一个或多个出现。
5.? 匹配零次或一次出现。
6.。 匹配任何字符,除了空。
7.| 用“OR”来指定多个选项。
8.[] 用于指定一个匹配列表,您尝试匹配列表中的任何一个字符。
9.[^] 用于指定一个不匹配的列表,您尝试匹配除列表中的字符以外的任何字符。
10.() 用于将表达式分组为一个子表达式。
11.{M} 匹配m次。
12.{M,} 至少匹配m次。
13.{M,N} 至少匹配m次,但不多于n次。
14.\ n n是1到9之间的数字。在遇到\ n之前匹配在()内找到的第n个子表达式。
15.[..] 匹配一个可以多于一个字符的整理元素。
15.[:] 匹配字符类。
16.[==] 匹配等价类。
17.\ d 匹配一个数字字符。
18.\ D 匹配一个非数字字符。
19.\ w 匹配包括下划线的任何单词字符。
20.\ W 匹配任何非单词字符。
21.\ s 匹配任何空白字符,包括空格,制表符,换页符等等。
22.\ S 匹配任何非空白字符。
23.\A 在换行符之前匹配字符串的开头或匹配字符串的末尾。
24.\Z 匹配字符串的末尾。
25.*? 匹配前面的模式零次或多次发生。
26.+? 匹配前面的模式一个或多个事件。
27.?? 匹配前面的模式零次或一次出现。
28.{N}? 匹配前面的模式n次。
29.{N,}? 匹配前面的模式至少n次。
30.{N,M}? 匹配前面的模式至少n次,但不超过m次。

说了一堆文绉绉的,现在开始实例演练了。

 
select * from tmp ;
ID             STR
-------     -------------
like        a9999
like        a9c
like        A7007
like        123a34cc
substr      123,234,345
substr      12,34.56:78
substr      123456789
instr       192.168.0.1
replace     (020)12345678
replace     001517729C28

regexp_like 例子:

语法:regexp_like(source_char,pattern,match_parameter)

select str from tmp where id=‘like‘ and regexp_like(str,‘A\d+‘,‘i‘); -- ‘i‘ 忽略大小写
STR
-------------
a9999
a9c
A7007
123a34cc
select str from tmp where id=‘like‘ and regexp_like(str, ‘a\d+‘);
STR
-------------
a9999
a9c
123a34cc

select str from tmp where id=‘like‘ and regexp_like(str,‘^a\d+‘);
STR
-------------
a9999
a9c
select str from tmp where id=‘like‘ and regexp_like(str,‘^a\d+$‘);
STR
-------------
a9999

regexp_substr 例子:

col str format a15;
select
  str,
  regexp_substr(str,‘[^,]+‘)     str,
  regexp_substr(str,‘[^,]+‘,1,1) str,
  regexp_substr(str,‘[^,]+‘,1,2) str,  -- occurrence 第几个匹配组
  regexp_substr(str,‘[^,]+‘,2,1) str   -- position 从第几个字符开始匹配
from tmp
where id=‘substr‘;
STR             STR             STR             STR             STR
--------------- --------------- --------------- --------------- ---------------
123,234,345     123             123             234             23
12,34.56:78     12              12              34.56:78        2
123456789       123456789       123456789                       23456789
 
select
  str,
  regexp_substr(str,‘\d‘)        str,
  regexp_substr(str,‘\d+‘  ,1,1) str,
  regexp_substr(str,‘\d{2}‘,1,2) str,
  regexp_substr(str,‘\d{3}‘,2,1) str
from tmp     
where id=‘substr‘;
STR             STR             STR             STR             STR
--------------- --------------- --------------- --------------- ---------------
123,234,345     1               123             23              234
12,34.56:78     1               12              34
123456789       1               123456789       34              234
 
 
select regexp_substr(‘123456789‘,‘\d‘,1,level) str  --取出每位数字,有时这也是行转列的方式
from dual
connect by level<=9
STR
---------------
1
2
3
4
5
6
7
8
9

regex_instr 例子:

col ind format 9999;
select
  str,
  regexp_instr(str,‘\.‘    ) ind ,
  regexp_instr(str,‘\.‘,1,2) ind ,
  regexp_instr(str,‘\.‘,5,2) ind
from tmp where id=‘instr‘;
STR               IND   IND   IND
--------------- ----- ----- -----
192.168.0.1         4     8    10
     
select
  regexp_instr(‘192.168.0.1‘,‘\.‘,1,level) ind ,  -- 点号. 所在的位置
  regexp_instr(‘192.168.0.1‘,‘\d‘,1,level) ind    -- 每个数字的位置
from dual
connect by level <=  9
  IND   IND
----- -----
    4     1
    8     2
   10     3
    0     5
    0     6
    0     7
    0     9
    0    11
    0     0

regex_replace 例子:

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

综合应用的例子:

col row_line format a30;
with sudoku as (
  select ‘020000080568179234090000010030040050040205090070080040050000060289634175010000020‘ as line
  from dual
),
tmp as (
  select regexp_substr(line,‘\d{9}‘,1,level) row_line,
  level col
  from sudoku
  connect by level<=9
)
select regexp_replace( row_line ,‘(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)‘,‘\1 \2 \3 \4 \5 \6 \7 \8 \9‘) row_line
from tmp
 
ROW_LINE
------------------------------
0 2 0 0 0 0 0 8 0
5 6 8 1 7 9 2 3 4
0 9 0 0 0 0 0 1 0
0 3 0 0 4 0 0 5 0
0 4 0 2 0 5 0 9 0
0 7 0 0 8 0 0 4 0
0 5 0 0 0 0 0 6 0
2 8 9 6 3 4 1 7 5
0 1 0 0 0 0 0 2 0






https://www.cnblogs.com/lxl57610/p/8227599.html

oracle的正则表达式,

原文:https://www.cnblogs.com/thomasbc/p/12526913.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶