在Oracle的11g版本中引入了REGEXP_COUNT函数,使用这个函数可以统计字符串出现的次数,小观一下。
1.REGEXP_COUNT函数语法参考
REGEXP_COUNT (source_char, pattern [, position [, match_param]])
2.先看一下使用最少参数的效果(仅使用前两个参数)
1)得到字符串中小写字母“a”的出现次数
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting.', 'a') "Count 'a'" from dual;
Count 'a'
----------
2
sys@ora11g> select regexp_count ('THE PRO-NIECE WAS BORN TODAY, SO EXCITING!', 'a') "Count 'a'" from dual;
Count 'a'
----------
0
3.大小写敏感匹配
不加其余参数的情况下,等同于下面的全参数形式。表示对字母大小写敏感匹配(最后一个参数“c”表示大小写敏感)。
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting.', 'a', 1, 'c') "Count 'a' case-sensitive" from dual;
Count 'a' case-sensitive
------------------------
2
sys@ora11g> select regexp_count ('THE PRO-NIECE WAS BORN TODAY, SO EXCITING!', 'a', 1, 'c') "Count 'a' case-sensitive" from dual;
Count 'a' case-sensitive
------------------------
0
4.大小写不敏感匹配
若意欲同时匹配大写字母“A”和小写字母“a”,可以启用“i”参数,表示大小写不敏感。
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting.', 'a', 1, 'i') "Count 'a' case-insensitive" from dual;
Count 'a' case-insensitive
--------------------------
2
sys@ora11g> select regexp_count ('THE PRO-NIECE WAS BORN TODAY, SO EXCITING!', 'a', 1, 'i') "Count 'a' case-insensitive" from dual;
Count 'a' case-insensitive
--------------------------
2
5.从指定位置进行检索
倒数第二个参数表示开始检索关键字的位置,如下例中的17表示从字符串的第17个字符处开始检索字母a(不区分大小写)。
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting!', 'a', 17, 'i') "Count 'a'" from dual;
Count 'a'
----------
1
6.Oracle官方文档参考链接
http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/functions135.htm#SQLRF20014
7.小结
几近人性化的函数给Oracle 11g增色添辉不少,抛砖完毕。
Good luck.
secooler
10.01.07
-- The End --