Tabular IQ
Back to Function Index

REGEXP_COUNT()

Overview

REGEXP_COUNT() returns the number of times a regular expression matches in a string. Use it to count what a value holds: the digits in a phone number, the separators in a code, the order numbers named in a description. To test only whether the pattern matches, use REGEXP_MATCHES(); to read the match itself, use REGEXP_EXTRACT().

Function Format

REGEXP_COUNT(string, pattern)

Return Value

REGEXP_COUNT() returns an integer value

Examples


REGEXP_COUNT('a1b22c333', '[0-9]+')       = 3
REGEXP_COUNT('a1b22c333', '[0-9]')        = 6
REGEXP_COUNT('abc', '[0-9]')              = 0

// Matches do not overlap
REGEXP_COUNT('aaaa', 'aa')                = 2

// (?i) at the start of the pattern ignores case
REGEXP_COUNT('ABCABCAXYaxy', 'A.')        = 3
REGEXP_COUNT('ABCABCAXYaxy', '(?i)A.')    = 4

// Rows whose contact field holds more than one e-mail address
REGEXP_COUNT(contact, '@') > 1

// How many purchase orders a description names
REGEXP_COUNT(description, 'PO-[0-9]+')

Notes

  • The matches are counted from left to right and do not overlap: each search starts where the match before it ended.
  • When the pattern matches nowhere, the result is 0.
  • Matching is case-sensitive. (?i) at the start of the pattern ignores case. REGEXP_COUNT() takes no options argument.
  • A pattern that can match nothing at all, such as x*, is counted once at every place it does so: REGEXP_COUNT('abc', 'x*') is 4. Write x+ to count the places an x stands.
  • \d, \w and \s stand for a digit, a letter, digit or underscore, and a blank, of the unaccented alphabet. An accented letter counts as one character, but not as a \w.
  • A pattern cannot refer back to one of its own groups or look ahead.
  • A pattern that is not valid is refused, with the reason, when the formula is checked.
  • If the string or the pattern is null, the result is null.
  • The function reads the same on a project table, on a table of a connected database and in a report.

See Also