在 Oracle 中使用正则表达式
Oracle使用正则表达式离不开这4个函数:
1。regexp_like
2。regexp_substr
3。regexp_instr
4。regexp_replace
看函数名称大概就能猜到有什么用了。
regexp_like 只能用于条件表达式,和 like 类似,但是使用的正则表达式进行匹配,语法很简单:
regexp_substr 函数,和 substr 类似,用于拾取合符正则表达式描述的字符子串,语法如下:
regexp_instr 函数,和 instr 类似,用于标定符合正则表达式的字符子串的开始位置,语法如下:
regexp_replace 函数,和 replace 类似,用于替换符合正则表达式的字符串,语法如下:
这里解析一下几个参数的含义:
1。source_char,输入的字符串,可以是列名或者字符串常量、变量。搜索字符串。可以是任意的数据类型char,VARCHAR2,nchar,CLOB,NCLOB类型
2。pattern,正则表达式。
3。match_parameter,匹配选项。
取值范围: i:大小写不敏感; c:大小写敏感;n:点号 . 不匹配换行符号;m:多行模式;x:扩展模式,忽略正则表达式中的空白字符。
4。position,标识从第几个字符开始正则表达式匹配。可选。搜索在字符串中的开始位置。如果省略,默认为1,这是第一个位置的字符串。
5。occurrence,标识第几个匹配组。可选。它是模式字符串中的第n个匹配位置。如果省略,默认为1。
6。replace_string,替换的字符串。
值 | 描述 |
^ | 匹配一个字符串的开始。如果与“m” 的match_parameter一起使用,则匹配表达式中任何位置的行的开头。 |
$ | 匹配字符串的结尾。如果与“m” 的match_parameter一起使用,则匹配表达式中任何位置的行的末尾。 |
* | 匹配零个或多个。 |
+ | 匹配一个或多个出现。 |
? | 匹配零次或一次出现。 |
。 | 匹配任何字符,除了空。 |
| | 用“OR”来指定多个选项。 |
[] | 用于指定一个匹配列表,您尝试匹配列表中的任何一个字符。 |
[^] | 用于指定一个不匹配的列表,您尝试匹配除列表中的字符以外的任何字符。 |
() | 用于将表达式分组为一个子表达式。 |
{M} | 匹配m次。 |
{M,} | 至少匹配m次。 |
{M,N} | 至少匹配m次,但不多于n次。 |
\ n | n是1到9之间的数字。在遇到\ n之前匹配在()内找到的第n个子表达式。 |
[..] | 匹配一个可以多于一个字符的整理元素。 |
[:] | 匹配字符类。 |
[==] | 匹配等价类。 |
\ d | 匹配一个数字字符。 |
\ D | 匹配一个非数字字符。 |
\ w | 匹配包括下划线的任何单词字符。 |
\ W | 匹配任何非单词字符。 |
\ s | 匹配任何空白字符,包括空格,制表符,换页符等等。 |
\ S | 匹配任何非空白字符。 |
\A | 在换行符之前匹配字符串的开头或匹配字符串的末尾。 |
\Z | 匹配字符串的末尾。 |
*? | 匹配前面的模式零次或多次发生。 |
+? | 匹配前面的模式一个或多个事件。 |
?? | 匹配前面的模式零次或一次出现。 |
{N}? | 匹配前面的模式n次。 |
{N,}? | 匹配前面的模式至少n次。 |
{N,M}? | 匹配前面的模式至少n次,但不超过m次。 |
说了一堆文绉绉的,现在开始实例演练了,在此之前先建好一个表。
create table tmp as
with data as (
select 'like' as id ,'a9999' as str from dual union all
select 'like' ,'a9c' from dual union all
select 'like' ,'A7007' from dual union all
select 'like' ,'123a34cc' from dual union all
select 'substr' ,'123,234,345' from dual union all
select 'substr' ,'12,34.56:78' from dual union all
select 'substr' ,'' from dual union all
select 'instr' ,'192.168.0.1' from dual union all
select 'replace' ,'(020)12345678' from dual union all
select 'replace' ,'001517729C28' from dual
)
select * from data ; 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 例子:
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('','\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,'','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 '' 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
在 Oracle 中使用正则表达式的更多相关文章
- Oracle中的正则表达式
检查约束 --密码的长度必须在3-6 --年龄必须在1-120 --性别只能是男或女 --电话号码必须满足电话的格式: 手机格式,座机格式 drop table test; select * from ...
- 初识Oracle中的正则表达式
Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.regexp_substr 3.regexp_instr 4.regexp_replace
- Oracle 中使用正则表达式
Oracle使用正则表达式离不开这4个函数: 1.regexp_like select t3.cert_no from table_name t3 where regexp_like(t3.cert_ ...
- ORACLE中的支持正则表达式的函数
ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与INSTR的功能相似3,REGEXP_SUBSTR :与SUBS ...
- Oracle中正则表达式的使用
Oracle10开始支持正则表达式. ORACLE中的支持正则表达式的函数主要有下面四个: 1. REGEXP_LIKE : 与LIKE的功能相似 2. REGEXP_INSTR : ...
- Oracle中REGEXP_SUBSTR及其它支持正则表达式的内置函数小结
Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下:在oracle中,使用一条语句实现将'17,20,23'拆分成'17','20','23'的集合. REGEXP_SUBSTR函数格 ...
- Oracle中使用REGEXP_SUBSTR,regexp_replace函数
REGEXP_SUBSTR函数格式如下: function REGEXP_SUBSTR(String, pattern, position, occurrence, modifier)__srcstr ...
- 任督二脉之Shell中的正则表达式
VBird说学习Linux,掌握了Shell和正则就相当于打通了任督二脉,此后能力的成长才会突飞猛进. Shell的基础学习之前已经总结了一篇博客:http://www.cnblogs.com/jyz ...
- Oracle中的数据类型和数据类型之间的转换
Oracle中的数据类型 /* ORACLE 中的数据类型: char 长度固定 范围:1-2000 VARCHAR2 长度可变 范围:1-4000 LONG 长度可变 最大的范围2gb 长字符类型 ...
随机推荐
- SVN 中项目名字全是红色 和 文件夹图标上没标记绿钩解决办法
没有绿色标记卸载Tortoise 小乌龟重装 ! 红色名字是 原来idea当前的project用了版本控制器, 那么建到这个project下面的所有项目就都是加入到版本控制里面的, ...
- 用arthas查看JVM已加载的类及方法信息
1.sc:“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息,这个命令支持的参数有 [d].[E].[f] 和 [x:]. [d] 输出当前类的详细信 ...
- 优雅的阅读CSDN博客
CSDN现在似乎不强制登录了2333.但是广告多了也是碍眼的不行...将下列css添加到stylus中就行了. 代码转自xzz的博客. 自己修改了一下,屏蔽了登录弹出框. .article_conte ...
- LOJ6029 [雅礼集训2017]市场
看到区间整除操作,直觉是不会除太多次就变成全 \(1\). 然而现在还有加操作. 我也不知道为什么,当一个节点的 \(\lfloor\frac{mx}{d}\rfloor=\lfloor\frac{m ...
- 重构 改善既有代码的设计 (Martin Fowler 著)
第1章 重构, 第一个案例 1.1 起点 1.2 重构的第一步 1.3 分解并重组 statement() 1.4 运用多态取代与价格相关的条件逻辑 1.5 结语 第2章 重构原则 2.1 何谓重构 ...
- Prometheus神器之监控K8s集群
Prometheus 简介 Prometheus是SoundCloud开源的一款开源软件.它的实现参考了Google内部的监控实现,与源自Google的Kubernetes结合起来非常合适.另外相比i ...
- WEBAPI 设置上传文件大小
参考资料:https://stackoverflow.com/questions/33399267/cors-error-when-uploading-larger-files https:// ...
- Java内功心法,深入解析面向对象
什么是对象 对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位.一个对象由一组属性和对这组属性进行操作的一组服务组成. 类的实例化可生成对象,一个对象的生命周期包括三个阶段:生成.使用 ...
- Python基础24
import 与 from import 知乎上说的简洁明了,zhihu.com/question/38857862 from import, 导入之后就能拿来用了,直接用!到处用!
- TensorFlow、numpy、matplotlib、基本操作
一.常量的定义 import tensorflow as tf #类比 语法 api 原理 #基础数据类型 运算符 流程 字典 数组 data1 = tf.constant(2,dtype=tf.in ...