【11g】使用REGEXP_COUNT函式統計字串出現的次數

secooler發表於2010-01-07
在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官方文件參考連結


7.小結
幾近人性化的函式給Oracle 11g增色添輝不少,拋磚完畢。

Good luck.

secooler
10.01.07

-- The End --

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-624634/,如需轉載,請註明出處,否則將追究法律責任。

相關文章