regexp_count 関数

適用対象:check marked yes Databricks SQL Databricks Runtime 11.3 LTS 以降

strregexp パターンと一致する回数を返します。

構文

regexp_count( str, regexp )

引数

  • str: 照合する STRING 式。
  • regexp: パターンを含む STRING 式。

返品

INTEGER

regexp文字列は正規表現である必要があります。 サポートされている構文については 、正規表現 を参照してください。

リテラルを使用する場合は、エスケープ文字の前処理を回避するために raw-literal (r プレフィックス) を使います。

引数のどちらかが NULL の場合、結果は NULL になります。

一般的なエラー状態

パターンの出現回数をカウントする

パターン Ste(v|ph)en は、 StevenStephenの両方に一致します。

> 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 input returns 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