适用于:
Databricks SQL
Databricks Runtime 11.3 LTS 及更高版本
返回 str 与 regexp 模式匹配的次数。
语法
regexp_count( str, regexp )
参数
-
str:要匹配的STRING表达式。 -
regexp:具有模式的STRING表达式。
返回
INTEGER。
字符串 regexp 必须是正则表达式。 有关支持的语法,请参阅 正则表达式 。
使用文本时,请使用 raw-literal (r prefix) 以避免转义字符预处理。
如果任一参数为 NULL,则结果为 NULL。
常见错误条件
示例
计算模式的出现次数
该模式 Ste(v|ph)en 同时匹配 Steven 和 Stephen。
> SELECT regexp_count('Steven Jones and Stephen Smith are the best players', 'Ste(v|ph)en');
2
对字符串中的数字进行计数
> SELECT regexp_count('There are 10 cats, 20 dogs, and 30 birds', r'\d+');
3
对字符串中的单词进行计数
> SELECT regexp_count('one two three four', r'\w+');
4
不区分大小写
(?i)使用内联标志忽略大小写。
> SELECT regexp_count('Yes yes YES', r'(?i)yes');
3
无匹配返回 0
> SELECT regexp_count('Mary had a little lamb', 'Ste(v|ph)en');
0
NULL 输入返回 NULL
> SELECT regexp_count(NULL, 'Ste(v|ph)en');
NULL
> SELECT regexp_count('Mary had a little lamb', NULL);
NULL
无效的正则表达式模式
> SELECT regexp_count('abc', '[invalid');
Error: INVALID_PARAMETER_VALUE.PATTERN