REGEXP_COUNT

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

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

Syntax 

1regexp_count(<string>, <pattern>)

Arguments 

Required 

  • <string>: The string to search within.
  • <pattern>: The regular expression pattern to count.

Returns 

Returns an integer representing the number of times the pattern occurs.

Considerations 

  • Returns 0 when no matches are found.
  • Counts non-overlapping matches.
  • For detailed regex syntax, see Regular Expression Syntax.

Examples 

Count Digits 

Count individual digits in a string.

1SELECT regexp_count('a1b2c3', '\d') AS result;

Returns 3.

Count Words 

Count words in a string.

1SELECT regexp_count('The quick brown fox', '\w+') AS result;

Returns 4.

No Matches 

Returns 0 when pattern is not found.

1SELECT regexp_count('abcdef', '\d') AS result;

Returns 0.

Count Specific Patterns 

Count occurrences of a specific pattern.

1SELECT regexp_count('banana', 'an') AS result;

Returns 2.

Use for Validation 

Check for expected number of matches.

1SELECT phone FROM contacts
2WHERE regexp_count(phone, '\d') = 10;

Returns phone numbers with exactly 10 digits.

Related Documentation