Use the REGEXP_COUNT function to count the number of occurrences of a string

In the 11g version of Oracle, the REGEXP_COUNT function was introduced. Using this function, the number of occurrences of strings can be counted. Take a look.


1. For the syntax of the REGEXP_COUNT function, refer to
REGEXP_COUNT (source_char, pattern [, position [, match_param]])


2. Let’s take a look at the effect of using the fewest parameters (only the first two parameters are used)
1) Get the lowercase letter “a” in the string Occurrence
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. Case-sensitive matching
If no other parameters are added, it is equivalent to the following full-parameter form. Indicates case-sensitive matching of letters (the last parameter "c" means case-sensitive).
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. Case-insensitive matching
If you want to match uppercase "A" and lowercase "a" at the same time, you can enable the "i" parameter to indicate case insensitivity.
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting.', 'a', 1, 'i') "Count 'a'




                         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. Retrieve from the specified position The
penultimate parameter indicates the position to start retrieving the keyword, as shown in the following example The 17 in means searches for the letter a (case-insensitive) starting at the 17th character of the string.
sys@ora11g> select regexp_count ('The pro-niece was born today, so exciting!', 'a', 17, 'i') "Count 'a'" from dual;


Count 'a'
------ ----
         1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326243174&siteId=291194637