RegexpCount

The RegexpCount function returns the number of times a regular expression pattern is matched in a string.

Syntax

RegexpCount(string, pattern)

Function arguments:

  • string (required): The string to search.
  • pattern (required): The pattern to match within the string.
📘

When the regular expression you want to use contains a slash, quotation or other special character, you will need to use a backslash () to escape the special character.

Notes

  • When any of the arguments are Null, the function returns Null.
  • Regular expressions vary by database. Check the documentation of your data platform for syntax.
  • RegexpCount only counts non-overlapping matches. Searching resumes at the first character after a match.
  • RegexpCount only counts matches of the entire pattern, not the number of groups captured.

Examples

RegexpCount("aaaabbbbcccc", "a")

Returns 4.

RegexpCount("aaa", "aa")

Returns 1. The function finds one complete match of "aa". Searching resumes after the second character in "aaa", so the final a is not counted as part of another match.

RegexpCount("aaaabbbbcccc", "a+")

Returns 1. The entire sequence "aaaa" is matched as a single occurrence.

RegexpCount("aaaabbbbcccc", "[a-z]+")

Returns 1. The entire string is one continuous sequence of lowercase letters.

RegexpCount("123abc456", "[0-9]+")

Returns 2. There are two separate sequences of digits: "123" and "456".

RegexpCount("aazabzacz", "a[a-z]?z")

Returns 3. This matches "aaz", "abz", and "acz".

RegexpCount("aazabzacz", "a([a-z]?)z")

Returns 3. The function counts matches of the entire pattern, not the number of groups captured.