Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Oracle REGEXP_COUNT function

10 August 2020

Oracle REGEXP_COUNT function

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:
MeaningDescription
^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.
\nn 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
\dCorresponds to the digital symbol.
\DCorresponds to a non-digital symbol.
\wCorresponds to the text symbol.
\WCorresponds to a non-text symbol.
\sCorresponds to the space character.
\SDoesn’t match the space character.
\ACorresponds to the beginning of a line or corresponds to the end of a line before a new line character.
\ZCorresponds 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:
MeaningDescription
‘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_idlast_name
1000AeroSmith
2000Joy
3000Scorpions

 

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_idlast_nametotal
1000AeroSmith4
2000Joy1
3000Scorpions3

 

Oracle regular expression regexp count

 
Tags: , , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...

5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...

The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...

Preamble​​MS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...

First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...

Preamble​​Atom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...

Preamble​​MongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...

FlexibilityOne of the most advertised features of MongoDB is its flexibility.  Flexibility, however, is a double-edged sword. More flexibility means more...

Preamble​​SQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...

Preamble​​Writing an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...

  What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....

Preamble​​If you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...