# 模式匹配语法

通配符模式可用于 Opus 中的许多函数，包括：

* 使用 [查找文件](/directory-opus/guan-fang-shou-ce/readme.zh-1/readme.zh-2/readme.zh.md) 工具搜索文件时（匹配文件名或文件内容或元数据）
* 在文件列表中[过滤](/directory-opus/guan-fang-shou-ce/readme.zh-1/readme.zh-2.md)文件时
* 在文件列表中[选择](/directory-opus/guan-fang-shou-ce/readme.zh-1/readme.zh-1/simple_wildcard_selection.zh.md)文件时
* [过滤文件操作](/directory-opus/guan-fang-shou-ce/readme.zh-2/readme.zh-4.md)（如复制或移动）时

除了[正则表达式](/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh/regular_expression_syntax.zh.md)之外，Opus 还支持更简单的通配符系统，称为*标准模式匹配*系统。使用通配符时，您需要指定一个由文本和特殊**通配符**混合而成的模式。每个标记用于匹配目标字符串中的一个或多个字符。举个简单的例子，模式 **\*.doc** 匹配任何以文本 *.doc* 结尾的内容（换句话说，就是文档文件）。

| 标记        | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- |
| **#**     | <p><strong>#</strong> 后面的字符或表达式重复 0 次或多次。</p><p>例如：<br><strong>a#xb</strong> 匹配 <em>ab</em>、<em>axb</em>、<em>axxb</em>、<em>axxxb</em> 等。<br><strong>a#(xyz)b</strong> 匹配 <em>ab</em>、<em>axyzb</em>、<em>axyzxyzb</em> 等。</p>                                                                                                                                                                                                                                                                                        |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **?**     | <p>匹配任意单个字符。</p><p>例如：<br><strong>a?b</strong> 匹配 <em>aab</em>、<em>abb</em>、<em>acb</em>、<em>adb</em> 等。它<strong>不</strong>匹配 <em>ab</em>。</p>                                                                                                                                                                                                                                                                                                                                                                      |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **\|**    | <p>用于分隔多个表达式，其中任何一个都可以匹配（实际上是一个<strong>或</strong>运算符）。</p><p>例如：<br><strong>a(x                                                                                                                                                                                                                                                                                                                                                                                                                                     | y                                                                                                 | z)b</strong> 匹配 <em>axb</em>、<em>ayb</em> 和 <em>azb</em>。<br><strong>\*.(gif                                                                                                                             | bmp                                                                                         | jpg)</strong> 匹配任何以 <em>.gif</em>、<em>.bmp</em> 或 <em>.jpg</em> 结尾的内容。</p> |
| **\~**    | <p>这是<strong>非</strong>运算符，它否定后面的表达式。</p><p>例如：<br><strong>~~(\*.doc)</strong> 匹配任何不以 <em>.doc</em> 结尾的内容<br><strong>~~(\*.(gif                                                                                                                                                                                                                                                                                                                                                                                     | bmp))</strong> 匹配任何不以 <em>.gif</em> 或 <em>.bmp</em> 结尾的内容</p>                                     |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **()**    | <p>括号用于将多个字符组合成一个表达式。以上面的 <strong>a#(xyz)b</strong> 为例，括号用于将 <em>xyz</em> 组合成一个表达式。如果没有括号（即 <strong>a#xyzb</strong>），则只有 <em>x</em> 会被视为跟在 <strong>#</strong> 字符之后 - <em>yzb</em> 将被视为文本。<br>括号可以嵌套（如上面的 <strong>\~</strong> 示例），以将多个表达式组合成一个更大的表达式。</p><p>例如：<br><strong>NEW-((\*.doc)                                                                                                                                                                                                                             | (**backup**))</strong> 匹配以 <em>NEW-</em> 开头并以 <em>.doc</em> 结尾或后跟字符串 <em>*backup*</em> 的任何内容。</p> |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **\[]**   | <p>匹配指定字符集中的任何单个字符。<br>您可以将字符集指定为单个字符（例如 <strong>\[abdfg]</strong>）或字符范围（例如 <strong>\[a-j]</strong>）或多个范围。</p><p>例如：<br><strong>\[abc]</strong> 匹配 <em>a</em>、<em>b</em> 或 <em>c</em><br><strong>\[af-j]</strong> 匹配 <em>a</em>、<em>f</em>、<em>g</em>、<em>h</em>、<em>i</em> 或 <em>j</em><br><strong>\[a-dh-kq-]</strong> 匹配 <em>a</em>、<em>b</em>、<em>c</em>、<em>d</em>、<em>h</em>、<em>i</em>、<em>j</em>、<em>k</em> 或 <em>q</em> 之后的任何字符<br><strong>IMGP#\[0-9].jpg</strong> 匹配 <em>IMGP0158.jpg</em>（或任何其它数字）。</p> |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **\[\~]** | <p>匹配指定字符集中<strong>不</strong>存在的任何字符。有关如何定义集合的信息，请参阅 <strong>\[]</strong>。</p><p>例如：<br><strong>\[\~pqr]</strong> 匹配除 <em>p</em>、<em>q</em> 或 <em>r</em> 之外的任何字符</p>                                                                                                                                                                                                                                                                                                                                                |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **\***    | <p>匹配任意数量的字符，包括零个字符。这是 <strong>#?</strong> 的同义词。<br><strong>*.doc</strong> 等价于 <strong>#?.doc</strong>。<br>例如：<br><strong>abc*xyz.\*q</strong> 匹配以 <em>abc</em> 开头、以 <em>xyz</em> 结尾并以 <em>q</em> 结尾的任何文件扩展名的任何文件名。</p>                                                                                                                                                                                                                                                                                             |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **'**     | <p>撇号称为<em>转义字符</em>。</p><p>模式字符串中不是通配符的任何字符都将被视为文本 - 文本必须与目标字符串中的字符完全匹配。但是，如果确实希望将通配符用作文本本身，则必须通过在其前面添加撇号来对其进行转义。</p><p>例如：<br>如上所述，<strong>a#xb</strong> 匹配 <em>ab</em>、<em>axb</em>、<em>axxb</em>、<em>axxxb</em> 等。<br><strong>a'#xb</strong> 将只匹配 <em>a#xb</em></p>                                                                                                                                                                                                                                             |                                                                                                   |                                                                                                                                                                                                          |                                                                                             |                                                                            |
| **grp:**  | <p>使用通配符匹配文件名时，这是一种引用由 <a href="/pages/5N1kfBOsGAsdrXWpnoQz">文件类型组</a> 定义的所有文件扩展名的简写方法。<br>例如，<strong>Images</strong>组可能包含文件扩展名 <em>.jpg</em>、<em>.bmp</em> 和 <em>.gif</em>。如果您希望某个命令自动选择这些类型的文件，可以使用以下命令：<br><strong>Select \*.(jpg                                                                                                                                                                                                                                                                                | bmp                                                                                               | gif)</strong><br>更好的方法是使用 <strong>grp:</strong> 标记自动包含该组中的所有文件扩展名：<br><strong>Select grp:Images</strong></p><p>这更容易阅读（和输入！），而且如果您曾经在相关组中添加或删除文件类型，则任何引用该组的模式都将自动反映新的更改。</p><p>例如：<br><strong>(grp:Images | grp:Documents)</strong> 匹配<strong>Images</strong>或<strong>Documents</strong>文件类型组中的任何文件</p> |                                                                            |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chaoses-ib.gitbook.io/directory-opus/guan-fang-shou-ce/readme.zh-10/readme.zh/pattern_matching_syntax.zh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
