REGEXP_SUBSTR
パターン pat で指定された正規表現に一致する文字列 expr の部分文字列を返します。一致がない場合は NULL を返します。expr または pat が NULL の場合、戻り値は NULL です。
- REGEXP_SUBSTR はキャプチャグループ(丸括弧
()で定義されるサブパターン)の抽出をサポートしていません。特定のキャプチャグループではなく、一致した部分文字列全体を返します。
SELECT REGEXP_SUBSTR('abc123', '(\w+)(\d+)');
-- Returns 'abc123' (the entire match), not 'abc' or '123'.
-- Alternative Solution: Use string functions like SUBSTRING and REGEXP_INSTR to manually extract the desired portion of the string:
SELECT SUBSTRING('abc123', 1, REGEXP_INSTR('abc123', '\d+') - 1);
-- Returns 'abc' (extracts the part before the digits).
SELECT SUBSTRING('abc123', REGEXP_INSTR('abc123', '\d+'));
-- Returns '123' (extracts the digits).
- REGEXP_SUBSTR は、
eパラメータ(Snowflake でキャプチャグループを抽出するために使用)や、返すキャプチャグループを指定するgroup_numパラメータをサポートしていません。
SELECT REGEXP_SUBSTR('abc123', '(\w+)(\d+)', 1, 1, 'e', 1);
-- Error: TiDB Cloud Lake does not support the 'e' parameter or capture group extraction.
-- Alternative Solution: Use string functions like SUBSTRING and LOCATE to manually extract the desired substring, or preprocess the data with external tools (e.g., Python) to extract capture groups before querying.
SELECT SUBSTRING(
REGEXP_SUBSTR('letters:abc,numbers:123', 'letters:[a-z]+,numbers:[0-9]+'),
LOCATE('letters:', 'letters:abc,numbers:123') + 8,
LOCATE(',', 'letters:abc,numbers:123') - (LOCATE('letters:', 'letters:abc,numbers:123') + 8)
);
-- Returns 'abc'
構文
REGEXP_SUBSTR(<expr>, <pat[, pos[, occurrence[, match_type]]]>)
引数
戻り値の型
VARCHAR
例
SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+');
+----------------------------------------+
| REGEXP_SUBSTR('abc def ghi', '[a-z]+') |
+----------------------------------------+
| abc |
+----------------------------------------+
SELECT REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3);
+----------------------------------------------+
| REGEXP_SUBSTR('abc def ghi', '[a-z]+', 1, 3) |
+----------------------------------------------+
| ghi |
+----------------------------------------------+
SELECT REGEXP_SUBSTR('周 周周 周周周 周周周周', '周+', 2, 3);
+------------------------------------------------------------------+
| REGEXP_SUBSTR('周 周周 周周周 周周周周', '周+', 2, 3) |
+------------------------------------------------------------------+
| 周周周周 |
+------------------------------------------------------------------+