> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# YAMLRegExpTree 字典源

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

export const CloudNotSupportedBadge = () => {
  return <div className="cloudNotSupportedBadge">
            <div className="cloudNotSupportedIcon">
            <svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path strokeWidth="1.5" d="M6.33366 12.6666L12.3739 12.6667C13.6593 12.6667 14.7073 11.6187 14.7073 10.3334C14.7073 9.04804 13.6593 8.00003 12.3739 8.00003C12.3739 8.00003 12.3337 7.66659 12.0003 7.33325M10.667 5.33322C8.00033 2.33325 4.45395 4.78537 4.14195 6.68203C2.55728 6.7627 1.29395 8.06203 1.29395 9.6667C1.29395 11.3234 2.66699 12.6666 4.00033 12.6666" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
                <path strokeWidth="1.5" d="M2.66699 14L12.0003 4.66663" stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" />
            </svg>

        </div>
            Not supported in ClickHouse Cloud
        </div>;
};

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

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

<div id="configuration">
  ## 配置
</div>

```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` 目录下。 |

<div id="yaml-file-structure">
  ## YAML 文件结构
</div>

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`) 。字符串匹配按深度优先的方式进行：如果某个字符串匹配某个节点，则还会检查其子节点。匹配层级最深的节点属性优先，会覆盖父节点中同名的属性。

<div id="related-pages">
  ## 相关页面
</div>

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