> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# YAMLRegExpTree 字典源

> 将 YAML 文件配置为正则表达式树字典的数据源。

[ClickHouse Cloud 不支持此功能](https://clickhouse.com/docs/products/cloud/guides/cloud-compatibility.md#list-of-unsupported-features)

`YAMLRegExpTree` 字典源会从本地文件系统中的 YAML 文件加载正则表达式树。
它专门用于 [`regexp_tree`](https://clickhouse.com/docs/zh/reference/statements/create/dictionary/layouts/regexp-tree) 字典布局，
并为基于模式的查找 (例如 User-Agent 解析) 提供分层的正则表达式到属性映射。

> [!NOTE]
>
> `YAMLRegExpTree` 字典源仅在 ClickHouse Open Source 中可用。
> 对于 ClickHouse Cloud，请将字典导出为 CSV，并改用 [ClickHouse table 源](https://clickhouse.com/docs/zh/reference/statements/create/dictionary/sources/clickhouse.md) 进行加载。
> 详情请参见[在 ClickHouse Cloud 中使用 regexp\_tree 字典](https://clickhouse.com/docs/zh/reference/statements/create/dictionary/layouts/regexp-tree.md#use-regular-expression-tree-dictionary-in-clickhouse-cloud)。

## 配置

```sql theme={null}
CREATE DICTIONARY regexp_dict
(
    regexp String,
    name String,
    version String
)
PRIMARY KEY(regexp)
SOURCE(YAMLRegExpTree(PATH '/var/lib/clickhouse/user_files/regexp_tree.yaml'))
LAYOUT(regexp_tree)
LIFETIME(0);
```

设置字段：

| 设置 | 说明 |
| - | - |
| `PATH` | 包含正则表达式树的 YAML 文件的绝对路径。通过 DDL 创建时，该文件必须位于 `user_files` 目录下。 |

## YAML 文件结构

YAML 文件包含一组正则表达式树节点。每个节点都可以包含属性和子节点，从而形成层级结构：

```yaml theme={null}
- regexp: 'Linux/(\d+[\.\d]*).+tlinux'
  name: 'TencentOS'
  version: '\1'

- regexp: '\d+/tclwebkit(?:\d+[\.\d]*)'
  name: 'Android'
  versions:
    - regexp: '33/tclwebkit'
      version: '13'
    - regexp: '3[12]/tclwebkit'
      version: '12'
    - regexp: '30/tclwebkit'
      version: '11'
    - regexp: '29/tclwebkit'
      version: '10'
```

每个节点都具有以下结构：

* **`regexp`**：该节点的正则表达式。
* **attributes**：用户定义的字典属性 (例如 `name`、`version`) 。属性值可以包含对正则表达式中捕获组的**反向引用**，写作 `\1` 或 `$1` (数字 1-9) 。这些引用会在查询时被替换为匹配到的捕获组。
* **child nodes**：子节点列表，每个子节点都有自己的属性，并且还可以选择包含更多子节点。子节点列表的名称是任意的 (例如上面的 `versions`) 。字符串匹配按深度优先的方式进行：如果某个字符串匹配某个节点，则还会检查其子节点。匹配层级最深的节点属性优先，会覆盖父节点中同名的属性。

## 相关页面

* [regexp\_tree 字典布局](https://clickhouse.com/docs/zh/reference/statements/create/dictionary/layouts/regexp-tree.md) — 布局配置、查询示例和匹配模式
* [dictGet](https://clickhouse.com/docs/zh/reference/functions/regular-functions/ext-dict-functions.md#dictGet), [dictGetAll](https://clickhouse.com/docs/zh/reference/functions/regular-functions/ext-dict-functions.md#dictGetAll) — 查询 regexp tree 字典的函数
