Skip to content

🌐 Add LLM Translations #1706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,35 @@
title: REGEXP_SUBSTR
---

返回字符串 `expr` 中与正则表达式 `pat` 匹配的子字符串,如果没有匹配则返回 NULL。如果 `expr` 或 `pat` 为 NULL,则返回值为 NULL。
返回字符串 `expr` 中与正则表达式模式 `pat` 匹配的子串,若无匹配则返回 NULL。若 expr 或 pat 为 NULL,返回值也为 NULL。

- REGEXP_SUBSTR 不支持提取捕获组(由括号 `()` 定义的子模式)。它返回整个匹配的子串而非特定的捕获组。

```sql
SELECT REGEXP_SUBSTR('abc123', '(\w+)(\d+)');
-- 返回 'abc123'(整个匹配项),而非 'abc' 或 '123'。

-- 替代方案:使用 SUBSTRING 和 REGEXP_INSTR 等字符串函数手动提取所需部分:
SELECT SUBSTRING('abc123', 1, REGEXP_INSTR('abc123', '\d+') - 1);
-- 返回 'abc'(提取数字前的部分)。
SELECT SUBSTRING('abc123', REGEXP_INSTR('abc123', '\d+'));
-- 返回 '123'(提取数字部分)。
```

- REGEXP_SUBSTR 不支持 `e` 参数(Snowflake 中用于提取捕获组)或 `group_num` 参数来指定返回哪个捕获组。

```sql
SELECT REGEXP_SUBSTR('abc123', '(\w+)(\d+)', 1, 1, 'e', 1);
-- 错误:Databend 不支持 'e' 参数或捕获组提取。

-- 替代方案:使用 SUBSTRING 和 LOCATE 等字符串函数手动提取所需子串,或在查询前通过外部工具(如 Python)预处理数据以提取捕获组。
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)
);
-- 返回 'abc'
```

## 语法

Expand All @@ -12,13 +40,13 @@ REGEXP_SUBSTR(<expr>, <pat[, pos[, occurrence[, match_type]]]>)

## 参数

| 参数 | 描述 |
|------------|-----------------------------------------------------------------------------------------------------------|
| expr | 要匹配的字符串表达式 |
| pat | 正则表达式 |
| pos | 可选。在 `expr` 中开始搜索的位置。如果省略,默认值为 1。 |
| occurrence | 可选。要搜索的匹配项的出现次数。如果省略,默认值为 1。 |
| match_type | 可选。指定如何执行匹配的字符串。其含义与 REGEXP_LIKE() 中的描述相同。 |
| 参数 | 描述 |
|-------------|---------------------------------------------------------------------------------------------------|
| expr | 要匹配的字符串表达式 |
| pat | 正则表达式 |
| pos | 可选。在 expr 中开始搜索的位置。若省略,默认为 1。 |
| occurrence | 可选。要搜索的第几次匹配。若省略,默认为 1。 |
| match_type | 可选。指定匹配方式的字符串,其含义与 REGEXP_LIKE() 中描述的相同。 |

## 返回类型

Expand Down