REGEXP_COUNT
Function Description
The REGEXP_COUNT function is used to count the number of times substrings matching a specified regular expression pattern appear in a string. This function returns an integer representing the total number of matches. If the string is empty or no matches exist, it returns 0.
Syntax
Parameter Description
- string_expression: STRING, the string expression to be searched
- pattern_expression: STRING, Java regular expression pattern. See List of Supported Regular Expression Functions
Return Value
Return type: INTEGER
Returns the number of times the specified regular expression matches in the string. If the pattern does not match, returns 0.
Usage Examples
Example 1: Count Simple String Matches
Count the occurrences of hello in a string:
Execution Result:
Example 2: Count Digit Matches
Count the number of digits in a date string:
Execution Result:
In this example, the date string 2024-10-28 contains 3 digit sequences: 2024, 10, and 28.
Example 3: Count Character Class Patterns
Count the number of individual digit characters in a string:
Execution Result:
The string hello123world456 contains 6 digit characters (1, 2, 3, 4, 5, 6).
Example 4: Complex Pattern Matching
Apply different patterns on multiple strings:
Execution Result:
appleappears 3 times in the string- Vowels appear 5 times in "The quick brown fox"
- Digit characters appear 10 times in the phone number
Example 5: Edge Case Handling
Test empty strings and non-matching patterns:
Execution Result:
- Empty string returns 0
- Non-existent pattern returns 0
- Greedy quantifier
a+treats consecutiveaaaas one match, returning 1
