Introduction

Continuing the series of rarely used database features with:

The Oracle REGEXP_COUNT function is one of the most useful regular expression functions available in Oracle Database. It allows you to count how many times a pattern appears in a string using powerful regular expression matching.

It is widely used in:

  • Data validation
  • ETL processes
  • Data quality checks
  • Log analysis
  • Text parsing
  • Security auditing
  • Reporting
  • CSV and delimiter analysis
  • Email and phone validation

If you work with messy real-world data, REGEXP_COUNT can save enormous amounts of SQL code.

What is REGEXP_COUNT?

REGEXP_COUNT returns the number of times a regular expression pattern occurs in a string.

Basic Syntax

REGEXP_COUNT(
source_string,
pattern,
start_position,
match_parameter
)

Parameters Explained

ParameterDescription
source_stringText to search
patternRegular expression pattern
start_positionOptional starting position
match_parameterOptional matching behavior

Simple Example

Count vowels in a word

SELECT REGEXP_COUNT('DATABASE', '[AEIOU]') AS vowel_count FROM dual;
Output
4
SQL query displaying a REGEXP_COUNT function that counts the vowels in the word 'DATABASE', with the result showing a vowel count of 4.
Explanation:
  • [] defines a character class
  • [AEIOU] matches any vowel
  • Oracle counts occurrences

Case Sensitive vs Case Insensitive

Case Sensitive (Default)

SELECT REGEXP_COUNT('Oracle oracle ORACLE', 'oracle') FROM dual;
Output:
1

Only lowercase oracle matches.

SQL query displaying REGEXP_COUNT function results in a database interface, with the query to count occurrences of 'oracle' in a string.

Case Insensitive

SELECT REGEXP_COUNT(
'Oracle oracle ORACLE',
'oracle',
1,
'i'
)
FROM dual;
Output:
3

i means case-insensitive.

SQL query using REGEXP_COUNT function to count occurrences of the word 'oracle' in a case-insensitive manner, resulting in a count of 3.

Real-Life Example — Count Words in a Sentence

Suppose you want to count how many words exist in customer comments.

SELECT REGEXP_COUNT(
'Oracle databases are powerful',
'\S+'
) AS word_count
FROM dual;
Output:
4
Explanation:
PatternMeaning
\S+One or more non-space characters
SQL query output showing the result of counting words in the string 'Oracle databases are powerful', displaying a word count of 4.

Real-Life Example — Count Commas in CSV Data

Very common in ETL and data import validation.

SELECT REGEXP_COUNT(
'John,Smith,London,UK',
','
) AS comma_count
FROM dual;
SQL query showing usage of REGEXP_COUNT function to count commas in a string 'John,Smith,London,UK', with result displayed as COMMA_COUNT equal to 3.
Output:
3

Useful for validating CSV column consistency.

Count Number of Values in CSV

A practical trick:

Number of values = commas + 1

SELECT REGEXP_COUNT(
'John,Smith,London,UK',
','
) + 1 AS total_columns
FROM dual;
Output:
4
SQL query output showing total columns derived from a REGEXP_COUNT function.

Validate Email Structure

Suppose we want to identify rows containing email addresses.

Sample Table

CREATE TABLE customers (
customer_id NUMBER,
email VARCHAR2(100)
);
Table CUSTOMERS created.

Insert Data

INSERT INTO customers VALUES (1, 'john@gmail.com');
INSERT INTO customers VALUES (2, 'invalid-email');
INSERT INTO customers VALUES (3, 'mary@yahoo.com');
commit;
SQL script displaying INSERT commands to add customer email records and corresponding output messages indicating rows inserted.

Count Valid Emails

SELECT COUNT(*)
FROM customers
WHERE REGEXP_COUNT(
email,
'^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$'
) > 0;

Explanation:

SymbolMeaning
^Start of string
$End of string
+One or more
\.Literal dot
SQL query showing a SELECT statement that counts customer emails matching a regular expression pattern, with the query result displaying a count of 2.

Find Rows with Multiple Spaces

Very common in data cleansing projects.

SELECT REGEXP_COUNT(
'Oracle Database 23c',
' {2,}'
) AS multiple_spaces
FROM dual;
Output:
2

Explanation:

PatternMeaning
{2,}Two or more spaces
SQL query displaying the REGEXP_COUNT function counting multiple spaces in the string 'Oracle  Database  23c'.

Count Digits in a String

Useful for validating IDs or phone numbers.

SELECT REGEXP_COUNT(
'INV-2025-9988',
'[0-9]'
) AS digit_count
FROM dual;
Output:
8
SQL query showing the use of REGEXP_COUNT to count digits in a string 'INV-2025-9988', with a result of 8.

Count Special Characters

Security and password validation frequently require this.

SELECT REGEXP_COUNT(
'Welcome@2025!',
'[^A-Za-z0-9]'
) AS special_characters
FROM dual;
Output:
2

Explanation:

PatternMeaning
^ inside []NOT
[^A-Za-z0-9]Non-alphanumeric characters
SQL code snippet showing a query that counts special characters in the string 'Welcome@2025!'

Count Occurrences of a Word

SELECT REGEXP_COUNT(
'error warning error failed error',
'error'
) AS error_count
FROM dual;
Output:
3
SQL query using REGEXP_COUNT function to count occurrences of the word 'error' in a string, displaying a result of 3.

Count HTML Tags

Useful in content management systems.

SELECT REGEXP_COUNT(
'<p>Hello</p><div>World</div>',
'<[^>]+>'
) AS html_tag_count
FROM dual;
Output:
4
SQL query showing REGEXP_COUNT function counting HTML tags in a string.

Count Numeric Values in Mixed Data

SELECT REGEXP_COUNT(
'ABC123XYZ456',
'[0-9]+'
) AS numeric_groups
FROM dual;
Output:
2
Explanation:
  • 123
  • 456

are counted as separate groups.

SQL query using REGEXP_COUNT function to count numeric groups in the string 'ABC123XYZ456'.

Real-Life ETL Validation Example

Suppose bank account numbers must contain exactly 12 digits

Sample Data


CREATE TABLE accounts (
    account_no VARCHAR2(50)
);

INSERT INTO accounts VALUES ('123456789012');
INSERT INTO accounts VALUES ('ABC123');
INSERT INTO accounts VALUES ('999999999999');
commit;
Screenshot showing SQL commands for creating a table named 'accounts' and inserting values into it, with completion messages displayed.

Find Invalid Accounts

SELECT account_no
FROM accounts
WHERE REGEXP_COUNT(account_no, '^[0-9]{12}$') = 0;
Output:
ABC123
SQL query displaying the selection of account numbers from the accounts table, with a condition using REGEXP_COUNT to filter results.

Using Start Position

You can start searching from a specific character position.

SELECT REGEXP_COUNT(
'abcabcabc',
'abc',
4
)
FROM dual;
Output:
2

Oracle starts searching from position 4.

Screenshot of SQL query using REGEXP_COUNT to count occurrences of 'abc' in 'abcabcabc'. Query result shows the count as 2.

Match Parameters

ParameterMeaning
iCase-insensitive
cCase-sensitive
nDot matches newline
mMulti-line mode
xIgnore whitespace

Multi-Line Example

SELECT REGEXP_COUNT(
'ERROR
WARNING
ERROR',
'ERROR',
1,
'm'
)
FROM dual;
Output:
2
SQL code snippet demonstrating the usage of REGEXP_COUNT function to count occurrences of the word 'ERROR' in a string.

Real-Life Log File Analysis

Suppose application logs are stored in a table.

Table

CREATE TABLE app_logs (
log_text CLOB
);
Table APP_LOGS created.

Count Errors

SELECT REGEXP_COUNT(
log_text,
'ERROR'
) AS total_errors
FROM app_logs;
SQL query code snippet counting occurrences of 'ERROR' in app logs with script output showing 'no rows selected'.

Data Quality Use Cases

REGEXP_COUNT is heavily used in enterprise data quality systems.

Examples

ScenarioExample
Detect duplicate delimiters,,
Validate phone number lengthCount digits
Validate PAN/Tax IDsAlphanumeric checks
Detect malformed emailsEmail regex
Count invalid symbolsSecurity screening
Analyze text patternsNLP preprocessing
Validate CSV structureCount delimiters
Check password strengthCount symbols/numbers

Comparing REGEXP_COUNT vs LENGTH

Many developers use LENGTH(str) instead of REGEXP_COUNT(str, ‘,’)

Which is Better?

MethodAdvantage
REGEXP_COUNTMore readable and powerful
LENGTH/REPLACESlightly faster for simple chars

Use REGEXP_COUNT when pattern complexity matters.

Performance Considerations

Regular expressions are powerful but can be expensive.

Best Practices

Use Simple Patterns

Avoid overly complex regex.

Bad:

([A-Za-z0-9._%+-]+)+

Better:

[A-Za-z0-9._%+-]+

Avoid Regexp on Huge Datasets Without Filters

Instead of:

SELECT * FROM big_table WHERE REGEXP_COUNT(col, 'ERROR') > 0;

Use pre-filters:

SELECT * FROM big_table WHERE col LIKE '%ERROR%' AND REGEXP_COUNT(col, 'ERROR') > 0;

Common Regexp Symbols Cheat Sheet

SymbolMeaning
.Any character
*Zero or more
+One or more
?Optional
^Start
$End
[abc]Any listed char
[^abc]Not listed char
[0-9]Digit
[A-Z]Uppercase
\sWhitespace
\SNon-whitespace

Advanced Real-Life Example — Password Validation

Suppose password rules require:

  • Minimum 1 uppercase
  • Minimum 1 lowercase
  • Minimum 1 digit
  • Minimum 1 special character

Validation Query

SELECT password FROM users WHERE REGEXP_COUNT(password, '[A-Z]') >= 1 AND REGEXP_COUNT(password, '[a-z]') >= 1 AND REGEXP_COUNT(password, '[0-9]') >= 1 AND REGEXP_COUNT(password, '[^A-Za-z0-9]') >= 1;

This is widely used in security systems.

Summary

REGEXP_COUNT is extremely valuable for:

  • Data validation
  • Text analytics
  • Cleansing messy data
  • ETL pipelines
  • Security validation
  • Log analysis
  • CSV parsing
  • Pattern detection

Its biggest advantage is combining SQL querying with powerful pattern matching.

Most Useful Real-World Use Cases

Use CaseExample
Email validationCount valid patterns
CSV validationCount delimiters
Password validationCount digits/symbols
Log monitoringCount ERROR/WARNING
Data cleansingDetect extra spaces
Financial systemsValidate account numbers
Healthcare systemsValidate IDs
TelecomCount digits in phone numbers
RetailAnalyze customer comments
ETLDetect malformed records

Final Tips

Use

REGEXP_COUNT

When:

  • Pattern matching is needed
  • Data is messy
  • Validation rules are complex
  • Parsing semi-structured text

Avoid It When:

  • Simple LIKE is enough
  • Performance is critical on massive tables
  • Exact string matching is sufficient

possible topics for future articles, let me know your preference!

  1. Count vowels in employee names
  2. Count dots in IP addresses
  3. Count words in customer reviews
  4. Detect multiple commas
  5. Validate SSN format
  6. Count hashtags in social posts
  7. Count URLs in text
  8. Count repeated digits in phone numbers

Discover more from Radu Pârvu

Subscribe to get the latest posts sent to your email.


Leave a Reply

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Radu Pârvu

Subscribe now to keep reading and get access to the full archive.

Continue reading