Preamble
Oracle/PLSQL REGEXP_COUNT function calculates the number of occurrences of the template in the string. This function, introduced in Oracle 11g, allows you to count the number of times a substring occurs in a string using a regular expression pattern matching.
Oracle/PLSQL syntax REGEXP_COUNT function
REGEXP_COUNT( string_id, pattern_id [, start_position_id [, atch_parameter_id ] ] )
Parameters and arguments
- string_id – A search line. String can be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB or NCLOB.
- pattern_id – Template. Regular expression for comparison. It can be a combination of the following values:
Meaning | Description |
^ | Corresponds to the beginning of the line. When using match_parameter with m, corresponds to the beginning of the string anywhere within the expression. |
$ | Corresponds to the end of the line. When using match_parameter with m, it corresponds to the end of the string anywhere within the expression. |
* | Corresponds to zero or more occurrences. |
+ | Corresponds to one or more occurrences. |
? | Corresponds to zero or one entry. |
. | Corresponds to any character except NULL. |
| | Used as “OR” to specify more than one alternative. |
[ ] | It is used to specify a list of matches where you try to match any of the characters in the list. |
[^ ] | It is used to specify a nonmatching list where you try to match any character except for those on the list. |
( ) | Used for group expressions as subexpressions. |
{m} | Corresponds m times. |
{m,} | Matching at least m times. |
{m,n} | Matching at least m times, but not more than n times. |
\n | n is a number between 1 and 9. It corresponds to the n-th subexpression located in ( ) before \n. |
[..] | Corresponds to a single element mappings that can be more than one character. |
[::] | Meets the symbol class. |
[==] | Corresponds to the class of equivalence |
\d | Corresponds to the digital symbol. |
\D | Corresponds to a non-digital symbol. |
\w | Corresponds to the text symbol. |
\W | Corresponds to a non-text symbol. |
\s | Corresponds to the space character. |
\S | Doesn’t match the space character. |
\A | Corresponds to the beginning of a line or corresponds to the end of a line before a new line character. |
\Z | Corresponds to the end of the line. |
*? | Corresponds to the previous pattern of zero or more occurrences. |
+? | One or more entries correspond to the previous template. |
?? | Corresponds to the previous zero or one entry pattern. |
{n}? | Corresponds to the previous template n times. |
{n,}? | Corresponds to the previous template at least n times. |
{n,m}? | Corresponds to the previous template at least n times, but not more than m times. |
- start_position_id – It’s optional. This is the position on the line from which the search will begin. If this option is omitted, by default it is 1, which is the first position in the line.
- match_parameter_id – It’s optional. It allows you to change the matching behavior for the function REGEXP_COUNT. This can be a combination of the following values:
Meaning | Description |
‘c’ | Performs register-sensitive alignment. |
‘i’ | Performs case insensitive alignment. |
‘n’ | Allows a character period (.) to match the character of a new string. By default, the metasymic period. |
‘m’ | The expression assumes that there are several lines where ^ is the beginning of a line and $ is the end of a line, regardless of the position of these characters in the expression. By default, the expression is assumed to be on the same line. |
‘x’ | The symbols of spaces are ignored. By default, the space characters are the same as any other character. |
The function REGEXP_COUNT returns a numerical value.
If there are conflicting values for match_parameter, the REGEXP_COUNT function will use the last value.
If the REGEXP_COUNT function does not detect any pattern occurrence, it will return 0.
REGEXP_COUNT function in the following versions of Oracle / PLSQL
Oracle 12c, Oracle 11g
Example of a single symbol match
Let’s look at the simplest example. Let’s calculate how many times the ‘a’ character appears in a string.
For instance:
SELECT REGEXP_COUNT ('Aller Anfang ist schwer', 'a')
FROM dual;
-Result: 1
This example will return 1 because it counts the number of ‘a’ occurrences in a string. Since we haven’t specified the match_parameter value, the REGEXP_COUNT function will perform a case sensitive search, which means that the ‘A’ character will not be included into the count.
If we wanted to include both ‘a’ and ‘A’ in our result and perform a case insensitive search, we would change our query as follows:
SELECT REGEXP_COUNT ('Aller Anfang ist schwer', 'a', 1, 'i')
FROM dual;
-Result: 3
Now, since we have provided start_position = 1 and match_parameter = ‘i’, the query will return 3 as a result. This time the values ‘a’ and ‘A’ will be included into the result.
If we wanted to count the number of ‘a’ in a column, we could try something like this:
SELECT REGEXP_COUNT (last_name, 'a', 1, 'i') AS total
FROM contacts;
This query will count the number of ‘a’ or ‘A’ values in the last_name field from the contacts table.
Example of a multi-character match
Let’s see how we will use the REGEXP_COUNT function to match a multi-character pattern.
For example:
SELECT REGEXP_COUNT ('Gute Saat, gute Ernte', 'gute', 1, 'i')
FROM dual;
-Result: 2
In this example, we will return the number of times the word ‘gute’ appears in the string. The search is case-insensitive, so the result is 2 entries.
SELECT REGEXP_COUNT ('Gute Saat, gute Ernte', 'gute', 5, 'i')
FROM dual;
-Result: 1
In this example, the number of times the word ‘gute’ appears in the string starting from position 5 will be returned. In this case the result is 1 because it will skip the first 4 characters in the string before searching for the template.
Now let’s see how we are going to use the REGEXP_COUNT function with the table column and search for several characters. For example:
SELECT REGEXP_COUNT (other_comments, 'the', 1, 'i')
FROM contacts;
-Result: 3
In this example, we will count the number of times “the” appears in the other_comments field in the contacts table.
This is an example of comparing several alternatives
The next example we’ll look at involves using the | pattern. | pattern is used as an “OR” to specify multiple alternatives. For example:
SELECT REGEXP_COUNT ('AeroSmith', 'a|e|i|o|u').
FROM dual;
-Result: 3
This example will return 3 because it counts the number of vowels (a, e, i, o or u) in the string ‘AeroSmith’. Since we didn’t specify a match_parameter value, the REGEXP_COUNT function will perform case sensitive searches, which means that ‘A’ in ‘AeroSmith’ will not be counted.
We could modify our query to perform a case-insensitive search as follows:
SELECT REGEXP_COUNT ('AeroSmith', 'a|e|i|o|u', 1, 'i')
FROM dual;
-Result: 4
Now, since we have specified start_position = 1 and match_parameter = ‘i’, the query will return as result 4. This time ‘A’ in ‘AeroSmith’ will be included into the score.
Now let’s see how we will use this function with the column.
So, suppose we have a contact table with the following data:
contact_id | last_name |
1000 | AeroSmith |
2000 | Joy |
3000 | Scorpions |
Now let’s start the next request:
SELECT contact_id, last_name, REGEXP_COUNT (last_name, 'a|e|i|o|u', 1, 'i') AS total
FROM contacts;
The results to be returned by the request:
contact_id | last_name | total |
1000 | AeroSmith | 4 |
2000 | Joy | 1 |
3000 | Scorpions | 3 |
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Enteros and Cloud FinOps: Driving Database Performance and Observability in the Financial Sector
- 19 February 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Cost Estimation in the Tech Sector: How Enteros Leverages Logical Models and Cloud FinOps for Smarter Database Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enteros and Cloud FinOps: Transforming Database Performance and Observability in the Real Estate Sector
- 18 February 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Database Performance for EdTech Firms: How Enteros Enhances RevOps with Advanced Database Performance Software
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…