oracle10g 正则表达式语法

http://www.cnblogs.com/quange/archive/2010/2/25.html

在正则表达式中包含许多元字符。例如,上面的SQL语句中的“^”、“$”、“*”等元字符。下表例举出常用的正则表达式元字符
元字符

 含义

 例子

 
\

 转义符,匹配特殊字符
 \n,匹配换行符
\\,匹配反斜线“\”
\(,匹配左括号“(”
\),匹配右括号“)”
 
^

 匹配字符串开头位置
 ^A,匹配以A打头字符
 
$

 匹配字符串结尾位置
 A$,匹配以A结尾字符
 
*

 匹配前面字符0次或是多次
 ab*c,可以匹配ac、abc、abbc等等
 
+

 匹配前面字符1次或是多次
 ab+c,可以匹配abc、abbc、abbbc等等
 
?

 匹配前面字符0次或1次
 ab?c,可以匹配ac、abc
 
{n}

 匹配一个字符n次,n为整数
 ab{2}c,可以匹配abbc
 
{n,m}

 匹配一个字符至少n次,最多m次
 ab{2,3}c,可以匹配abbc、abbbc
 
(pattern)

 匹配指定模式的一个子表达式
 ab(a|b),可以匹配aba、abb
 
x|y

 匹配x或y
 ab|cd,可以匹配ab、cd
 
[:alphanum:]

 匹配字母、数字
 可以匹配字符0-9、A-Z和a-z
 
[:alpha:]

 匹配字母
 可以匹配字符A-Z和a-z
 
[:blank:]

 匹配空格或tab键
 
 
[:digit:]

 匹配数字0-9
 
 
[:graph:]

 匹配非空字符
 
 
[:lower:]

 匹配小写字母a-z
 
 
[:upper:]

 匹配大写字母A-Z
 
 
[:xdigit:]

 匹配十六进制数字0-9、A-F和a-f
 
 
[:punct:]

 匹配标点符号.,”等等
 
 
[:space:]

 匹配所有的空格符
 

posted @ 2010-02-25 16:49 全哥 阅读(49) 评论(0) 编辑Oracle10g -新特性- 正则表达式 关于正则表达式的基础介绍,可以参考《深入浅出之正则表达式》,Oracle10g中实现的正则表达式与之基本相同,详细内容可以参考《10g中的正则表达式》。Oracle10g提供了4个可以使用正则表达式的函数:
 
regexp_like
regexp_substr
regexp_instr
regexp_replace
 
在包sys.standard中可以看到这些函数的声明(见附录)。这里主要对这4个函数做一些小测试。
 
1. regexp_like
 
REGEXP_LIKE (srcstr, pattern, modifier )
 
__srcstr        :检索字符串
__pattern      :匹配模式
__modifier      :检索模式('i'不区分大小写进行检索;'c'区分大小写进行检索。默认为'c'。)
__return type :boolean
 
eg:
 
SQL> select 1 from dual where regexp_like('Alibaba-13930471222','^alibaba-[139|135|152][0-9]+{8}$');
         1
----------
 
SQL> select 1 from dual where regexp_like('Alibaba-13930471222','^alibaba-[139|135|152][0-9]+{8}$', 'c');
         1
----------
 
SQL> select 1 from dual where regexp_like('Alibaba-13930471222','^alibaba-[139|135|152][0-9]+{8}$', 'i');
         1
----------
         1

 
 
2. regexp_substr
 
REGEXP_SUBSTR(srcstr, pattern, position, occurrence, modifier)
 
__srcstr        :检索字符串
__pattern      :匹配模式
__position     :搜索srcstr的起始位置(默认为1)
__occurrence:搜索第几次出现匹配模式的字符串(默认为1)
__modifier     :检索模式('i'不区分大小写进行检索;'c'区分大小写进行检索。默认为'c'。)
 
eg:
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/showart_1009927.html', '[0-9]+') from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
30637
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/showart_1009927.html', '[0-9]+', 1) from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
30637
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/showart_1009927.html', '[0-9]+', 35) from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
1009927
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/showart_1009927.html', '[0-9]+', 1, 2) from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
1009927
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/showart_1009927.html', '[a-z_0-9]+', 35) from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
showart_1009927
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/Showart_1009927.html', '[a-z_0-9]+', 35) from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
howart_1009927
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/Showart_1009927.html', '[a-z_0-9]+', 35, 1, 'c') from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
howart_1009927
 
SQL> select regexp_substr('http://blog.chinaunix.net/u/30637/Showart_1009927.html', '[a-z_0-9]+', 35, 1, 'i') from dual;
 
REGEXP_SUBSTR('HTTP://BLOG.CHI
------------------------------
Showart_1009927

 
 
3. regexp_instr
 
REGEXP_INSTR(srcstr, pattern, position, occurrence, returnparam, modifier)
 
__srcstr         :检索字符串
__pattern       :匹配模式
__position       :搜索srcstr的起始位置(默认为1)
__occurrence  :搜索第几次出现匹配模式的字符串(默认为1)
__returnparam :返回该子串在srcstr中的位置(0表示头位置,1表示尾位置+1,默认为0。)
__modifier       :检索模式('i'不区分大小写进行检索;'c'区分大小写进行检索。默认为'c'。)
 
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+') from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            12
 
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 21) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            52
 
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 1, 2) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            52
                           
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 1, 1, 0) from dual;
 
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            12
 
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 1, 1, 1) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            20
                           
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 1, 2, 0) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            52
 
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[0-9]+', 1, 2, 1) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                            60
                           
SQL> select regexp_instr('http://shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 1, 1, 0) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                             8
 
SQL> select regexp_instr('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 1, 1, 0) from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                             9
 
SQL> select regexp_instr('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 1, 1, 0, 'i') from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                             8
 
SQL> select regexp_instr('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 1, 1, 0, 'c') from dual;
 
REGEXP_INSTR('HTTP://SHOP35741
------------------------------
                             9

 
 
4. regexp_replace
 
function REGEXP_REPLACE(srcstr, pattern, replacestr, position, occurrence, modifier)
 
__srcstr         :检索字符串
__pattern       :匹配模式
__replacestr   :新的子串(默认值为NULL)
__position       :srcstr的检索起始位置(默认为1)
__occurrence  :替换第几次出现匹配模式的字符串(默认为0)
__modifier       :检索模式('i'不区分大小写进行检索;'c'区分大小写进行检索。默认为'c'。)
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 'YCT') from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
--------------------------------------------------
http://SYCT.taobao.com/shop/xshop/wui_page-YCT.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 'YCT', 20) from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
----------------------------------------------------------
http://Shop35741645.taobao.com/shop/xshop/wui_page-YCT.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 'YCT', 1, 2) from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
----------------------------------------------------------
http://Shop35741645.taobao.com/shop/xshop/wui_page-YCT.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 'YCT', 1, 1) from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
-------------------------------------------------------
http://SYCT.taobao.com/shop/xshop/wui_page-16115021.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*[0-9]+', 'YCT', 1, 1, 'i') from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
------------------------------------------------------
http://YCT.taobao.com/shop/xshop/wui_page-16115021.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*([0-9])+', '\1', 1, 1, 'i') from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
----------------------------------------------------
http://5.taobao.com/shop/xshop/wui_page-16115021.htm
 
SQL> select regexp_replace('http://Shop35741645.taobao.com/shop/xshop/wui_page-16115021.htm', '[a-z]*([0-9]+)', '\1', 1, 1, 'i') from dual;
 
REGEXP_REPLACE('HTTP://SHOP357
-----------------------------------------------------------
http://35741645.taobao.com/shop/xshop/wui_page-16115021.htm

 
 
5. 附录
 
-- REGEXP_LIKE --
function REGEXP_LIKE (srcstr   VARCHAR2 CHARACTER SET ANY_CS,
                      pattern  VARCHAR2 CHARACTER SET srcstr%CHARSET,
                      modifier VARCHAR2 DEFAULT NULL)
  return BOOLEAN;
  pragma FIPSFLAG('REGEXP_LIKE', 1452);
 
function REGEXP_LIKE (srcstr   CLOB CHARACTER SET ANY_CS,
                      pattern  VARCHAR2 CHARACTER SET srcstr%CHARSET,
                      modifier VARCHAR2 DEFAULT NULL)
  return BOOLEAN;
  pragma FIPSFLAG('REGEXP_LIKE', 1452);
 
-- REGEXP_INSTR --
function REGEXP_INSTR(srcstr      VARCHAR2 CHARACTER SET ANY_CS,
                      pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                      position    PLS_INTEGER := 1,
                      occurrence  PLS_INTEGER := 1,
                      returnparam PLS_INTEGER := 0,
                      modifier    VARCHAR2 DEFAULT NULL)
  return PLS_INTEGER;
  pragma FIPSFLAG('REGEXP_INSTR', 1452);
 
function REGEXP_INSTR(srcstr      CLOB CHARACTER SET ANY_CS,
                      pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                      position    INTEGER := 1,
                      occurrence  INTEGER := 1,
                      returnparam PLS_INTEGER := 0,
                      modifier    VARCHAR2 DEFAULT NULL)
  return INTEGER;
  pragma FIPSFLAG('REGEXP_INSTR', 1452);
 
-- REGEXP_SUBSTR --
function REGEXP_SUBSTR(srcstr      VARCHAR2 CHARACTER SET ANY_CS,
                       pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                       position    PLS_INTEGER := 1,
                       occurrence  PLS_INTEGER := 1,
                       modifier    VARCHAR2 DEFAULT NULL)
  return VARCHAR2 CHARACTER SET srcstr%CHARSET;
  pragma FIPSFLAG('REGEXP_SUBSTR', 1452);
 
function REGEXP_SUBSTR(srcstr      CLOB CHARACTER SET ANY_CS,
                       pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                       position    INTEGER := 1,
                       occurrence  INTEGER := 1,
                       modifier    VARCHAR2 DEFAULT NULL)
  return CLOB CHARACTER SET srcstr%CHARSET;
  pragma FIPSFLAG('REGEXP_SUBSTR', 1452);
 
-- REGEXP_REPLACE --
function REGEXP_REPLACE(srcstr      VARCHAR2 CHARACTER SET ANY_CS,
                        pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                        replacestr  VARCHAR2 CHARACTER SET srcstr%CHARSET
                                      DEFAULT NULL,
                        position    PLS_INTEGER := 1,
                        occurrence  PLS_INTEGER := 0,
                        modifier    VARCHAR2 DEFAULT NULL)
  return VARCHAR2 CHARACTER SET srcstr%CHARSET;
  pragma FIPSFLAG('REGEXP_REPLACE', 1452);
 
function REGEXP_REPLACE(srcstr      CLOB CHARACTER SET ANY_CS,
                        pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                        replacestr  CLOB CHARACTER SET srcstr%CHARSET
                                      DEFAULT NULL,
                        position    INTEGER := 1,
                        occurrence  INTEGER := 0,
                        modifier    VARCHAR2 DEFAULT NULL)
  return CLOB CHARACTER SET srcstr%CHARSET;
  pragma FIPSFLAG('REGEXP_REPLACE', 1452);
 
function REGEXP_REPLACE(srcstr      CLOB CHARACTER SET ANY_CS,
                        pattern     VARCHAR2 CHARACTER SET srcstr%CHARSET,
                        replacestr  VARCHAR2 CHARACTER SET srcstr%CHARSET
                                      DEFAULT NULL,
                        position    INTEGER := 1,
                        occurrence  INTEGER := 0,
                        modifier    VARCHAR2 DEFAULT NULL)
  return CLOB CHARACTER SET srcstr%CHARSET;
  pragma FIPSFLAG('REGEXP_REPLACE', 1452);
 
-- End REGEXP Support --

posted @ 2010-02-25 16:17 全哥 阅读(70) 评论(0) 编辑Oracle10g利用rownum取得排序后的部分行序列 我们使用oracle的人都知道可以通过rownum伪列得到查询结果序列前面的指定的行,为了下面更好的进行说明问题,我们先来创建一个数据表table1:

create table table1

(AAA integer primary key,

BBB varchar(30));

然后在table1中插入9条数据:

insert into table1 values (8, 'good');

insert into table1 values (7, 'morning');

insert into table1 values (20, 'afternoon');

insert into table1 values (2, 'have');

insert into table1 values (19, 'boy');

insert into table1 values (30, 'girl');

insert into table1 values (15, 'left');

insert into table1 values (26, 'think');

insert into table1 values (98, 'beautiful');

commit;

现在使用:

Select * from table1 where rownum < 4;

来得到前三个行。

AAA     BBB

8                     good

7                     morning

20                   afternoon

这没有问题,但如果你对rownum使用了大于号(>),则查询的结果集一定是空的。如:

Select * from table1 where rownum > 1;

无论表中有多少数据,都不会返回任何的数据。我们什么时候会用到rownum大于一个数字进行查询呢,这里先卖一个关子,后面再说。

再来说说排序和rownum的关系:

如果使用

Select * from table1 where rownum < 4 Order by AAA;

我这里查询出来的结果是:

AAA     BBB

7                      morning

8                      good

20                     afternoon

和前面没有Order By时的结果集数据是一样的,只是排了一个序,而我们期望的结果是:

AAA     BBB

2                      have

7                      morning

8                      good

那为什么会是这样的一个结果,而又如何才能得到我们期望的结果呢?我们先要搞清楚rownum是如何生成的。现在使用下面的语句查询一下:

select t.*, rownum from table1 t where rownum < 4 order by AAA ;

AAA     BBB      ROWNUM

7                      morning            2

8                      good                 1

20                    afternoon          3

发现rownum并不是按1,2,3的顺序排列的,为什么会是这样的一个结果呢?带着这个问题,我们再使用下面的这个语句进行查询:

select t.*, rownum, rowid from table1 t;

AAA             BBB             Rownum         RowID

8                      good     1                                  AAAY8QAABAAAVIaAAA

7                      morning            2                                  AAAY8QAABAAAVIaAAB

20                     afternoon           3                                  AAAY8QAABAAAVIaAAC

2                      have                  4                                  AAAY8QAABAAAVIaAAD

19                     boy                   5                                  AAAY8QAABAAAVIaAAE

30                     girl                    6                                  AAAY8QAABAAAVIaAAF

15                     left                    7                                  AAAY8QAABAAAVIaAAG

26                     think     8                                  AAAY8QAABAAAVIaAAH

98                     beautiful            9                                  AAAY8QAABAAAVIaAAI

看到这个结果我们猜想,oracle是插入数据的同时就为每一行建立了一个惟一的rowid,并且是按插入的顺序排序的,而rownum是按RowID进行排序的。为了证明我们的猜想是正确的,我们先删除AAA为19的行,再插入另一个AAA为34的行,语句分别如下:

delete from table1 where aaa = 19;

insert into table1 values (34, 'like');

现在我们再使用select t.*, rownum, rowid from table1 t;进行查询:

AAA           BBB           Rownum        RowID

8                      good                 1                                  AAAY8QAABAAAVIaAAA

7                      morning                        2                                  AAAY8QAABAAAVIaAAB

20                     afternoon                       3                                  AAAY8QAABAAAVIaAAC

2                      have                              4                                  AAAY8QAABAAAVIaAAD

30                     girl                                5                                  AAAY8QAABAAAVIaAAF

15                     left                                6                                  AAAY8QAABAAAVIaAAG

26                     think                 7                                  AAAY8QAABAAAVIaAAH

98                     beautiful                        8                                  AAAY8QAABAAAVIaAAI

34            like          9                    AAAY8QAABAAAVIaAAJ

结果证明我们的猜想是正确的:oracle是插入数据的同时就为每一行建立了一个惟一的rowid,并且是按插入的顺序排序的,而rownum是按RowID进行排序的。

现在问题就出来了,既然rownum是按照rowid进行排序的,而rowid我们是不能改变的,也就是说我们不能改变rownum的排序方式,那么如何才能取得排序后的前n行呢?

为了得到我们期望的排序后的前n行数据,我们使用子查询来实现,具体就是查询另一个查询语句返回的结果,sql语句如下:

select * from (select * from table1 t order by AAA) where rownum < 4 ;

返回的结果如下:

AAA           BBB

2                      have

7                      morning

8                      good

正是我们所期望的。

 

现在我们再来说说什么时候会用到rownum大于一个数字进行查询,而又如何实现。我们先来设想一下,在一个web页面上用一个table来显示数据,假设一页显示4行数据,现在我们要显示第二页的数据,我们怎么实现呢?dotnet的datagrid和gridview使用的非常笨拙低效的办法:把所有数据查出来,然后只显示指定的数据。为了更有效的处理这个问题,我们想只查询出来第二页的数据,这就要用到rownum的大于查询。我们期望使用

select * from (select * from table1 t order by AAA) where rownum > 4 and rownum <= 8;

来得到结果,可是前面说过,任何使用rownum大于一个数据的查询,都不会返回任何的结果集。那我们又如何才能得到我们期望的结果呢?这就要使用一点技巧了,先使用:

select * from (select * from table1 t order by AAA) where rownum <= 8;

返回排序后的前8行数据,但只是这样我们仍无法再过滤掉前面的四行数据。为此我们要使用这个查询的外层查询的临时rownum(可能有点拗口,再解释一下,我们要使用这个rownum不是表table1的rownum,而查询(select * from table1 t order by AAA)查询结果集在查询时生成的rownum),并对它重命名,然后再对这个返回结果进行查询,得到我们期望的结果,查询语句如下:

Select * from (select a.*, rownum rn from (select * from table1 t order by AAA) where rownum <= 8) where rn > 4;

AAA           BBB                   RN

20            afternoon            5

26            think               6

30            girl                 7

34            like                 8

这样,我们就成功的实现了利用rownum查询出排序后的中间部分行的功能。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值